Advertisement
Chiddix

MessageTransporter

Dec 23rd, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package me.rabrg.register.message;
  2.  
  3. import java.security.Security;
  4. import java.util.Date;
  5. import java.util.Properties;
  6.  
  7. import javax.mail.Message;
  8. import javax.mail.MessagingException;
  9. import javax.mail.Session;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12.  
  13. import com.sun.mail.smtp.SMTPTransport;
  14.  
  15. /**
  16.  * A class which transports messages.
  17.  * @author Ryan Greene
  18.  *
  19.  */
  20. @SuppressWarnings("restriction")
  21. public class MessageTransporter {
  22.  
  23.     /**
  24.      * The SSL socket factory used in the properties.
  25.      */
  26.     private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  27.  
  28.     static {
  29.         Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  30.     }
  31.  
  32.     /**
  33.      * The username of the account used to transport messages.
  34.      */
  35.     private final String username;
  36.  
  37.     /**
  38.      * The session used to create and transport messages.
  39.      */
  40.     private final Session session;
  41.  
  42.     /**
  43.      * The transport used to transport mesages.
  44.      */
  45.     private final SMTPTransport transport;
  46.  
  47.     /**
  48.      * Constructs a new message transporter with the specified username and password.
  49.      * @param username The username of the account used to transport messages.
  50.      * @param password The password of the account used to transport messages.
  51.      * @throws MessagingException If an exception was thrown.
  52.      */
  53.     public static MessageTransporter createMessageTransporter(final String username, final String password) throws MessagingException {
  54.         return new MessageTransporter(username, password);
  55.     }
  56.  
  57.     /**
  58.      * Constructs a new message transporter with the specified username and password.
  59.      * @param username The username of the account used to transport messages.
  60.      * @param password The password of the account used to transport messages.
  61.      * @throws MessagingException If an exception was thrown.
  62.      */
  63.     private MessageTransporter(final String username, final String password) throws MessagingException {
  64.         this.username = username;
  65.        
  66.         final Properties properties = System.getProperties();
  67.         properties.setProperty("mail.smtps.host", "smtp.gmail.com");
  68.         properties.setProperty("mail.smtp.socketFactory.class", MessageTransporter.SSL_FACTORY);
  69.         properties.setProperty("mail.smtp.socketFactory.fallback", "false");
  70.         properties.setProperty("mail.smtp.port", "465");
  71.         properties.setProperty("mail.smtp.socketFactory.port", "465");
  72.         properties.setProperty("mail.smtps.auth", "true");
  73.         properties.put("mail.smtps.quitwait", "false"); // wait
  74.        
  75.         session = Session.getInstance(properties, null);
  76.         transport = (SMTPTransport) session.getTransport("smtps");
  77.         transport.connect("smtp.gmail.com", username, password);
  78.     }
  79.  
  80.     /**
  81.      * Sends a message to the specified recipient with the specified subject and test.
  82.      * @param recipient The recipient receiving the message.
  83.      * @param subject The subject of the message.
  84.      * @param text The text of the message.
  85.      * @return Whether or not the message was sent successfully.
  86.      */
  87.     protected boolean send(final String recipient, final String subject, final String text) {
  88.         final MimeMessage message = new MimeMessage(session);
  89.        
  90.         try {
  91.             message.setFrom(new InternetAddress(username + "@gmail.com"));
  92.             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
  93.             message.setSubject(subject);
  94.             message.setText(text, "utf-8");
  95.             message.setSentDate(new Date());
  96.            
  97.             transport.sendMessage(message, message.getAllRecipients());
  98.             transport.close();
  99.             return true;
  100.         } catch (final MessagingException e) {
  101.             e.printStackTrace();
  102.         }
  103.         return false;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement