28/07/2013

[Java] Send mail

Here's an example on how to send HTML emails to multiple recipients, without attachments, via Java.

This Mailer class was written in Java 4 and relies upon the javax.mail.* libraries, providing a method send which takes 4 parameters:
  • List:rcptTo - "TO" recipients
  • List:ccs - "CC" recipients
  • String:subject - message subject
  • String:body - message body

 import java.util.Date;  
 import java.util.Hashtable;  
 import java.util.Iterator;  
 import java.util.List;  
 import java.util.Properties;  
   
 import javax.mail.Message;  
 import javax.mail.MessagingException;  
 import javax.mail.Session;  
 import javax.mail.Transport;  
 import javax.mail.internet.AddressException;  
 import javax.mail.internet.InternetAddress;  
 import javax.mail.internet.MimeMessage;  
   
 public class Mailer {  
   
   private String host = "yourhost.example.com";  
   private String port = "25";  
   private String mailFrom = "someone@example.com";  
        
   public void send(List rcptTo, List ccs, String subject, String body)  
   throws AddressException, MessagingException, IllegalArgumentException {  
     /*prepare properties to send the email*/  
       
     Properties sysProps = System.getProperties();  
       
     sysProps.setProperty("mail.smtp.host", this.host);  
     sysProps.setProperty("mail.smtp.port", this.port);  
       
     /*Refer to:http://edelstein.pebbles.cs.cmu.edu/jadeite/main.php?api=javamail&state=package&package=com.sun.mail.smtp  
      *If set to false, the QUIT command is sent and the connection is immediately closed. If set to true (the default), causes the transport to wait for the response to the QUIT command. */  
     sysProps.put("mail.smtp.quitwait", "false");  
       
     Session session = Session.getInstance(System.getProperties(), null);  
       
     MimeMessage msg = new MimeMessage(session);  
       
     /*add sender*/  
     msg.setFrom(new InternetAddress(this.mailFrom));  
       
     /*for each recipient*/  
     /*if there are none, raise error*/  
     if(rcptTo==null || rcptTo.isEmpty()){  
       throw new IllegalArgumentException(Mailer.class.getName()+" - error: rcptTo parameter cannot be null!");  
     }  
     Iterator iterRcptTo = rcptTo.iterator();  
       
     /*prepare recipients array*/  
     InternetAddress [] recipients = new InternetAddress[rcptTo.size()];  
     int i = 0;  
       
     while(iterRcptTo.hasNext()){  
       /*add recipient to message*/  
       recipients[i] = new InternetAddress((String)iterRcptTo.next());  
       i++;  
     }  
     /*mark them as "TO" recipients*/  
     msg.setRecipients(Message.RecipientType.TO, recipients);  
       
     /*same goes for CCs, if any*/  
     if(ccs!=null && !ccs.isEmpty()){  
       Iterator iterCCs = ccs.iterator();  
         
       /*prepare CCs array*/  
       InternetAddress [] ccss = new InternetAddress[ccs.size()];  
       int j = 0;  
         
       while(iterCCs.hasNext()){  
         /*add cc to message*/  
         ccss[j] = new InternetAddress((String)iterCCs.next());  
         j++;  
       }  
       /*mark them as "CC" recipients*/  
       msg.setRecipients(Message.RecipientType.CC, ccss);  
     }  
       
     /*set subject and body*/  
     msg.setSubject(subject);  
     msg.setContent(body,"text/html");  
     msg.setSentDate(new Date());  
   
           /*send the message*/  
     try{  
       Transport.send(msg);  
     }catch (MessagingException mex) {  
       mex.printStackTrace();   
       throw mex;  
     }  
   }  
 }  

No comments:

Post a Comment

With great power comes great responsibility