mauricioadl

Enviar e-mail com javamail

Nov 1st, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.94 KB | None | 0 0
  1. package br.com.lima;
  2.  
  3. import java.io.File;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Properties;
  7.  
  8. import javax.activation.DataHandler;
  9. import javax.activation.FileDataSource;
  10. import javax.mail.Authenticator;
  11. import javax.mail.Message;
  12. import javax.mail.MessagingException;
  13. import javax.mail.Multipart;
  14. import javax.mail.Part;
  15. import javax.mail.PasswordAuthentication;
  16. import javax.mail.Session;
  17. import javax.mail.Transport;
  18. import javax.mail.internet.InternetAddress;
  19. import javax.mail.internet.MimeBodyPart;
  20. import javax.mail.internet.MimeMessage;
  21. import javax.mail.internet.MimeMultipart;
  22.  
  23. /**
  24.  * class that encapsulates the sending of messages through the javax.mail.
  25.  *
  26.  * @author Mauricio Lima
  27.  * @date 18/07/2011
  28.  */
  29. public class SendMail {
  30.     private Session session;
  31.     private String user;
  32.     private String content = "text/html";
  33.  
  34.     public SendMail(boolean requiresAuthentication,
  35.             boolean requiresSSLConection, String host, String port,
  36.             final String user, final String password) {
  37.         super();
  38.         this.user = user;
  39.         Properties props = new Properties();
  40.         props.put("mail.smtp.auth", requiresAuthentication + "");
  41.         props.put("mail.smtp.port", port);
  42.         props.put("mail.host", host);
  43.         if (requiresSSLConection) {
  44.             props.put("mail.smtp.socketFactory.class",
  45.                     "javax.net.ssl.SSLSocketFactory");
  46.             props.put("mail.smtp.socketFactory.fallback", "false");
  47.         }
  48.  
  49.         Authenticator authenticator = new Authenticator() {
  50.             protected PasswordAuthentication getPasswordAuthentication() {
  51.                 return new PasswordAuthentication(user, password);
  52.             }
  53.         };
  54.  
  55.         this.session = Session.getInstance(props, authenticator);
  56.     }
  57.  
  58.     /**
  59.      * Sends a simple e-mail
  60.      * @param subject
  61.      * @param body
  62.      * @param recipientTO
  63.      * @throws MessagingException
  64.      */
  65.     public void sendMail(String subject, String body, String recipientTO) throws MessagingException{
  66.         sendMail(subject, body, new String[]{ recipientTO }, null, null, null, this.content);
  67.     }
  68.    
  69.     public void sendMail(String subject, String body, String recipientTO, File attachment) throws MessagingException{
  70.         if(attachment != null){
  71.             sendMail(subject, body, new String[]{ recipientTO }, null, null, new File[]{ attachment }, this.content);
  72.         } else {
  73.             sendMail(subject, body, new String[]{ recipientTO }, null, null, null, this.content);
  74.         }
  75.     }
  76.    
  77.     public void sendMail(String subject, String body, String recipientTO, File attachment, String[] recipientsBCC) throws MessagingException{
  78.         if(attachment != null){
  79.             sendMail(subject, body, new String[]{ recipientTO }, null, null, new File[] {attachment}, this.content);
  80.         } else {
  81.             sendMail(subject, body, new String[]{ recipientTO }, null, null, null, this.content);
  82.         }
  83.     }
  84.    
  85.     public void sendMail(String subject, String body, File attachment, List<String> recipientsBCC) throws MessagingException{
  86.         String[] recs = new String[recipientsBCC.size()];
  87.         for(int i = 0; i < recs.length; i++){
  88.             recs[i] = recipientsBCC.get(i);
  89.         }
  90.         if(attachment != null){
  91.             sendMail(subject, body, new String[]{ recs[0] }, null, recs, new File[] {attachment}, this.content);
  92.         } else {
  93.             sendMail(subject, body, new String[]{ recs[0] }, null, recs, null, this.content);
  94.         }
  95.     }
  96.    
  97.    
  98.    
  99.     /**
  100.      * Sends a complete e-mail
  101.      * @param subject
  102.      * @param body
  103.      * @param recipientsTO
  104.      * @param recipientsCC
  105.      * @param recipientsBCC
  106.      * @param attachments
  107.      * @param content
  108.      * @throws MessagingException
  109.      */
  110.     public void sendMail(String subject, String body, String[] recipientsTO,
  111.             String[] recipientsCC, String[] recipientsBCC, File[] attachments, String content)
  112.             throws MessagingException {
  113.         this.content = content;
  114.         // mount e-mail
  115.         MimeMessage message = new MimeMessage(session);
  116.         message.setFrom(new InternetAddress(this.user));
  117.         message.setSentDate(new Date());
  118.         message.setSubject(subject);
  119.         Multipart mp = new MimeMultipart();
  120.  
  121.         // mount body
  122.         MimeBodyPart mbp1 = new MimeBodyPart();
  123.         mbp1.setContent(body, this.content);
  124.         mp.addBodyPart(mbp1);
  125.  
  126.         // add attachments
  127.         if (attachments != null && attachments.length > 0) {
  128.             for (File file : attachments) {
  129.                 MimeBodyPart mime = new MimeBodyPart();
  130.                 FileDataSource fds = new FileDataSource(file);
  131.                 mime.setDisposition(Part.ATTACHMENT);
  132.                 mime.setDataHandler(new DataHandler(fds));
  133.                 mime.setFileName(fds.getName());
  134.                 mp.addBodyPart(mime);
  135.             }
  136.         }
  137.  
  138.         // add addresses TO
  139.         if (recipientsTO != null && recipientsTO.length > 0) {
  140.             InternetAddress[] destinationAddressesTO = new InternetAddress[recipientsTO.length];
  141.             for (int i = 0; i < destinationAddressesTO.length; i++) {
  142.                 destinationAddressesTO[i] = new InternetAddress();
  143.                 destinationAddressesTO[i].setAddress(recipientsTO[i]);
  144.             }
  145.             message.setRecipients(Message.RecipientType.TO,
  146.                     destinationAddressesTO);
  147.         }
  148.        
  149.         // add addresses CC
  150.         if (recipientsCC != null && recipientsCC.length > 0) {
  151.             InternetAddress[] destinationAddressesCC = new InternetAddress[recipientsCC.length];
  152.             for (int i = 0; i < destinationAddressesCC.length; i++) {
  153.                 destinationAddressesCC[i] = new InternetAddress();
  154.                 destinationAddressesCC[i].setAddress(recipientsCC[i]);
  155.             }
  156.             message.setRecipients(Message.RecipientType.CC,
  157.                     destinationAddressesCC);
  158.         }
  159.        
  160.         // add addresses BCC
  161.         if (recipientsBCC != null && recipientsBCC.length > 0) {
  162.             InternetAddress[] destinationAddressesBCC = new InternetAddress[recipientsBCC.length];
  163.             for (int i = 0; i < destinationAddressesBCC.length; i++) {
  164.                 destinationAddressesBCC[i] = new InternetAddress();
  165.                 destinationAddressesBCC[i].setAddress(recipientsBCC[i]);
  166.             }
  167.             message.setRecipients(Message.RecipientType.BCC,
  168.                     destinationAddressesBCC);
  169.         }
  170.         // add content and complete part
  171.         message.setContent(mp);
  172.        
  173.         // send the message
  174.         Transport.send(message);
  175.     }
  176.    
  177.     public String getContent() {
  178.         return content;
  179.     }
  180.    
  181.     public void setContent(String content) {
  182.         this.content = content;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment