Thursday, September 20, 2007

Sending mail from Java

import java.util.Properties;
import java.util.StringTokenizer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailSender {
    private static final java.lang.String MAIL_HOST = "mail.smtp.host";
    private Message message = null;
    private Properties props = null;
    private Multipart multipart = null;
    private String host = "";

    public MailSender() {
        String mailHost = ""; //can be read from property file 
        setHost(mailHost);
        initialize();

    }
    /**
     *param string 
     */
    public MailSender(String mailHost) {
        setHost(mailHost);
        initialize();
    }

    /**
     * intializing code 
     *
     */
    private void initialize() {
        props = System.getProperties();
        props.put(MAIL_HOST, getHost());
        Session session123 = Session.getInstance(props, null);
        message = new MimeMessage(session123);
        multipart = new MimeMultipart();
    }

    /**
     * @param string address of SMTP server 
     */
    protected void setHost(String string) {
        host = string;
    }

    /**
     * 
     * @return address of SMTP server being used
     */
    protected String getHost() {
        return host;
    }

    /**
     * 
     * @param address email address of sender 
     * @throws Exception
     */
    public void setFromRecipient(String address) throws Exception {
        message.setFrom(new InternetAddress(address));
    }

    /**
     * 
     * @param address email address of reciever for TO field,
     * if there are more than one reciever this method can be 
     *  used multiple times    
     * @throws Exception
     */
    public void addToRecipient(String address) throws Exception {
        message.addRecipient(
            Message.RecipientType.TO,
            new InternetAddress(address));
    }

    /**
     * 
     * @param address email address of reciever for CC field,
     * if there are more than one reciever this method can be 
     *  used multiple times
     * @throws Exception
     */
    public void addCCRecipient(String address) throws Exception {
        message.addRecipient(
            Message.RecipientType.CC,
            new InternetAddress(address));
    }

    /**
     * 
     * @param address email address of reciever for BCC field,
     * if there are more than one reciever this method can be 
     *  used multiple times
     * @throws Exception
     */
    public void addBCCRecipient(String address) throws Exception {
        message.addRecipient(
            Message.RecipientType.BCC,
            new InternetAddress(address));
    }

    /**
     * 
     * @param subject - subject for the mail
     * @throws Exception
     */
    public void setSubject(String subject) throws Exception {
        message.setSubject(subject);
    }

    /**
     * 
     * @param filename - the name of file to be attached
     * @param filePath - the path of file to be attached
     * @throws Exception
     */
    public void attachFile(String filename, String filePath) throws Exception {

        DataSource source = new FileDataSource(filePath);
        // Set the data handler to the attachment
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDataHandler(new DataHandler(source));
        // Set the filename
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
    }

    /**
     * 
     * @param messageText - message content
     * @throws Exception
     */
    public void setMessage(String messageText) throws Exception {
        // Create the message part
        BodyPart messageBodyPart = new MimeBodyPart();
        // Fill the message
        messageBodyPart.setText(messageText);
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
    }

    /**
     * 
     * @return
     */
    public void sendmail() {
        try {
            Transport.send(message);
            System.out.println("Mail sent successfully");

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("There was some problem, mail not sent. " + e.getMessage());

        }
    }

    /**
     * 
     * @param argv
     * @throws Exception
     */
    public static void main(String argv[]) throws Exception {

        MailSender mailSender = new MailSender("PUT_SERVER_IP_HERE");
        mailSender.setFromRecipient("I_AM_SENDER@domain.com");

        mailSender.setSubject("How are You?");

        mailSender.addToRecipient("receiver1@domain.com");
        mailSender.addToRecipient("receiver2@domain.co");

        mailSender.addCCRecipient("CCreceiver1@domain.com");
        mailSender.addCCRecipient("CCreceiver2@domain.com");

        mailSender.addBCCRecipient("BCCreceiver1@domain.com");
        mailSender.addBCCRecipient("BCCreceiver2@domain.com");

        mailSender.attachFile("File Name", "FILE PATH");
        mailSender.attachFile("Another File Name", "ANOTHER FILE PATH");

        mailSender.setMessage("Hello, \n\n How are.\n\nThanks.\n");

        mailSender.sendmail();

    }

}

Wednesday, September 19, 2007

Rounding a number in Java

Often we face a problem to round a number to a desired position. This program will round the number to a desired position.

import java.math.BigDecimal;

public class TestRound {
    /**
     * 
     * @param d
     * @param decimalPlace
     * @return
     */
    public static double round(double d, int decimalPlace) {
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_DOWN);
        return bd.doubleValue();
    }

    public static void main(String args[]) {
        double d = 3.1537;
        System.out.println(+ " : " + round(d, 2));
        System.out.println(+ " : " + round(d, 3));
    }
}