Monday 12 March 2007

Sending mail using Java Mail

In addition to the previous article, I shall provide here a slightly changed version of Sudhir Ancha's code as presented in his Sending Email From Your Application Using Java Mail article at javacomerice.com.

The only notable difference is the Charset = .. addition to the setContent() method of the message class, that allows sending the message using the iso8859-7 Greek character set.

package utils.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


/**
 * @class SendMail
 * Encapsulates a simple mail sending using a specific SMTP Server
 */
public class SendMail {
    // put you server here
    public static final String SMTP_HOST = "smtp.mydomain.com";

    public void post(String to, // recipient for the message
        String subject,         // subject
        String message,         // actual mesage text
        String from)            // address of sender
        throws MessagingException 
    {

        String[] recipients = new String[1];
        recipients[0] = to;

        postToMany(recipients, subject, message, from);
    }

    public void postToMany( String[] recipients, // list of recipients for the message
        String subject, // subject
        String message, // actual mesage text
        String from) // address of sender
        throws MessagingException 
    {
        boolean debug = false;

        //Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST);

        // create some properties and get the default Session
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) 
            addressTo[i] = new InternetAddress(recipients[i]);
        
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        // Content-type: text/html; charset=utf-8\r\n
        // Optional : You can also set your custom headers in the Email if you Want
        // msg.addHeader("Content-type", "text/html; charset=utf-8");

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain; Charset=iso8859-7");
        Transport.send(msg);
    }

    private static final String emailMsgTxt = "Δοκιμαστικό μύνημα σταλμένο από Java.";
    private static final String emailSubjectTxt = "EMail from Java";
    private static final String emailFromAddress = "me@mydomain.gr";
    private static final String[] emailList = {
            "he@hisdomain.com",
            "she@herdomain.gr" 
    };
    

    public static void main(String[] args) throws Exception 
    {
        SendMail smtpMailSender = new SendMail();
        smtpMailSender.post("me@mydomain.om", emailSubjectTxt, emailMsgTxt,
                            emailFromAddress);
        System.out.println("Sucessfully Sent mail to All Users");
    }
}

No comments :