Advertisement
Guest User

Java Mail

a guest
Jul 1st, 2015
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. package control;
  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.AddressException;
  11. import javax.mail.internet.InternetAddress;
  12. import javax.mail.internet.MimeMessage;
  13.  
  14. import com.sun.mail.smtp.SMTPTransport;
  15.  
  16. public class Mail {
  17.     private Mail() {
  18.     }
  19.  
  20.     /**
  21.      * Send email using GMail SMTP server.
  22.      *
  23.      * @param username
  24.      *            GMail username
  25.      * @param password
  26.      *            GMail password
  27.      * @param recipientEmail
  28.      *            TO recipient
  29.      * @param title
  30.      *            title of the message
  31.      * @param message
  32.      *            message to be sent
  33.      * @throws AddressException
  34.      *             if the email address parse failed
  35.      * @throws MessagingException
  36.      *             if the connection is dead or not in the connected state or if
  37.      *             the message is not a MimeMessage
  38.      */
  39.     public static void Send(final String username, final String password,
  40.             String recipientEmail, String title, String message)
  41.             throws AddressException, MessagingException {
  42.         Mail.Send(username, password, recipientEmail, "", title, message);
  43.     }
  44.  
  45.     /**
  46.      * Send email using GMail SMTP server.
  47.      *
  48.      * @param username
  49.      *            GMail username
  50.      * @param password
  51.      *            GMail password
  52.      * @param recipientEmail
  53.      *            TO recipient
  54.      * @param ccEmail
  55.      *            CC recipient. Can be empty if there is no CC recipient
  56.      * @param title
  57.      *            title of the message
  58.      * @param message
  59.      *            message to be sent
  60.      * @throws AddressException
  61.      *             if the email address parse failed
  62.      * @throws MessagingException
  63.      *             if the connection is dead or not in the connected state or if
  64.      *             the message is not a MimeMessage
  65.      */
  66.     public static void Send(final String username, final String password,
  67.             String recipientEmail, String ccEmail, String title, String message)
  68.             throws AddressException, MessagingException {
  69.         Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  70.         final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  71.  
  72.         // Get a Properties object
  73.         Properties props = System.getProperties();
  74.         props.setProperty("mail.smtps.host", "smtp.gmail.com");
  75.         props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
  76.         props.setProperty("mail.smtp.socketFactory.fallback", "false");
  77.         props.setProperty("mail.smtp.port", "465");
  78.         props.setProperty("mail.smtp.socketFactory.port", "465");
  79.         props.setProperty("mail.smtps.auth", "true");
  80.  
  81.         /*
  82.          * If set to false, the QUIT command is sent and the connection is
  83.          * immediately closed. If set to true (the default), causes the
  84.          * transport to wait for the response to the QUIT command.
  85.          *
  86.          * ref :
  87.          * http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp
  88.          * /package-summary.html
  89.          * http://forum.java.sun.com/thread.jspa?threadID=5205249 smtpsend.java
  90.          * - demo program from javamail
  91.          */
  92.         props.put("mail.smtps.quitwait", "false");
  93.  
  94.         Session session = Session.getInstance(props, null);
  95.  
  96.         // -- Create a new message --
  97.         final MimeMessage msg = new MimeMessage(session);
  98.  
  99.         // -- Set the FROM and TO fields --
  100.         msg.setFrom(new InternetAddress(username + "@gmail.com"));
  101.         msg.setRecipients(Message.RecipientType.TO,
  102.                 InternetAddress.parse(recipientEmail, false));
  103.  
  104.         if (ccEmail.length() > 0) {
  105.             msg.setRecipients(Message.RecipientType.CC,
  106.                     InternetAddress.parse(ccEmail, false));
  107.         }
  108.  
  109.         msg.setSubject(title);
  110.         msg.setText(message, "utf-8");
  111.         msg.setSentDate(new Date());
  112.  
  113.         SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
  114.  
  115.         t.connect("smtp.gmail.com", username, password);
  116.         t.sendMessage(msg, msg.getAllRecipients());
  117.         t.close();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement