· 9 years ago · Aug 31, 2016, 09:02 PM
1import com.sun.mail.smtp.SMTPTransport;
2import java.security.Security;
3import java.util.Date;
4import java.util.Properties;
5import javax.mail.Message;
6import javax.mail.MessagingException;
7import javax.mail.Session;
8import javax.mail.internet.AddressException;
9import javax.mail.internet.InternetAddress;
10import javax.mail.internet.MimeMessage;
11
12public class GoogleMail {
13 private GoogleMail() {}
14
15 /**
16 * Send email using GMail SMTP server.
17 *
18 * @param username GMail username
19 * @param password GMail password
20 * @param recipientEmail TO recipient
21 * @param title title of the message
22 * @param message message to be sent
23 * @throws AddressException if the email address parse failed
24 * @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage
25 */
26 public static void Send(final String username, final String password, String recipientEmail, String title, String message) throws AddressException, MessagingException {
27 GoogleMail.Send(username, password, recipientEmail, "", title, message);
28 }
29
30 /**
31 * Send email using GMail SMTP server.
32 *
33 * @param username GMail username
34 * @param password GMail password
35 * @param recipientEmail TO recipient
36 * @param ccEmail CC recipient. Can be empty if there is no CC recipient
37 * @param title title of the message
38 * @param message message to be sent
39 * @throws AddressException if the email address parse failed
40 * @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage
41 */
42 public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException {
43 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
44 final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
45
46 // Get a Properties object
47 Properties props = System.getProperties();
48 props.setProperty("mail.smtps.host", "smtp.gmail.com");
49 props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
50 props.setProperty("mail.smtp.socketFactory.fallback", "false");
51 props.setProperty("mail.smtp.port", "465");
52 props.setProperty("mail.smtp.socketFactory.port", "465");
53 props.setProperty("mail.smtps.auth", "true");
54
55 /*
56 If set to false, the QUIT command is sent and the connection is immediately closed. If set
57 to true (the default), causes the transport to wait for the response to the QUIT command.
58
59 ref : http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
60 http://forum.java.sun.com/thread.jspa?threadID=5205249
61 smtpsend.java - demo program from javamail
62 */
63 props.put("mail.smtps.quitwait", "false");
64
65 Session session = Session.getInstance(props, null);
66
67 // -- Create a new message --
68 final MimeMessage msg = new MimeMessage(session);
69
70 // -- Set the FROM and TO fields --
71 msg.setFrom(new InternetAddress(username + "@gmail.com"));
72 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
73
74 if (ccEmail.length() > 0) {
75 msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
76 }
77
78 msg.setSubject(title);
79 msg.setText(message, "utf-8");
80 msg.setSentDate(new Date());
81
82 SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
83
84 t.connect("smtp.gmail.com", username, password);
85 t.sendMessage(msg, msg.getAllRecipients());
86 t.close();
87 }
88}