JavaMail API – Sending email via Gmail SMTP example – Mkyong.com


email icon

In this article, we will show you how to send an email via Gmail SMTP server.

To send email in Java, we need JavaMail

pom.xml

com.sun.mail javax.mail 1.6.2

1. Gmail SMTP via TLS

SMTP = smtp.gmail.com Port = 587

SendEmailTLS.java

package com.mkyong; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class SendEmailTLS { public static void main(String[] args) { final String username = “username@gmail.com”; final String password = “password”; Properties prop = new Properties(); prop.put(“mail.smtp.host”, “smtp.gmail.com”); prop.put(“mail.smtp.port”, “587”); prop.put(“mail.smtp.auth”, “true”); prop.put(“mail.smtp.starttls.enable”, “true”); //TLS Session session = Session.getInstance(prop, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(“from@gmail.com”)); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(“to_username_a@gmail.com, to_username_b@yahoo.com”) ); message.setSubject(“Testing Gmail TLS”); message.setText(“Dear Mail Crawler,” + “nn Please do not spam my email!”); Transport.send(message); System.out.println(“Done”); } catch (MessagingException e) { e.printStackTrace(); } } }

2. Gmail via SSL

2.1 The logic is the same, just pass in different properties values.

SMTP = smtp.gmail.com Port = 465

SendEmailSSL.java

package com.mkyong; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class SendEmailSSL { public static void main(String[] args) { final String username = “username@gmail.com”; final String password = “password”; Properties prop = new Properties(); prop.put(“mail.smtp.host”, “smtp.gmail.com”); prop.put(“mail.smtp.port”, “465”); prop.put(“mail.smtp.auth”, “true”); prop.put(“mail.smtp.socketFactory.port”, “465”); prop.put(“mail.smtp.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”); Session session = Session.getInstance(prop, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(“from@gmail.com”)); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(“to_username_a@gmail.com, to_username_b@yahoo.com”) ); message.setSubject(“Testing Gmail SSL”); message.setText(“Dear Mail Crawler,” + “nn Please do not spam my email!”); Transport.send(message); System.out.println(“Done”); } catch (MessagingException e) { e.printStackTrace(); } } }

3. Application-specific password required

3.1 If 2-Step verification is ON. We will hits the following error message :

Caused by: javax.mail.AuthenticationFailedException: 534-5.7.9 Application-specific password required. Learn more at 534 5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor – gsmtp at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965) at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780) at javax.mail.Service.connect(Service.java:388) at javax.mail.Service.connect(Service.java:246) at javax.mail.Service.connect(Service.java:195) at javax.mail.Transport.send0(Transport.java:254) at javax.mail.Transport.send(Transport.java:124) at com.mkyong.calculator.SendEmail.main(SendEmail.java:41)

3.2 To fix it, follow this guide to create an App Password

URL : https://support.google.com/accounts/answer/185833?p=InvalidSecondFactor

app password

URL : https://myaccount.google.com/security

No difference in code, just puts the newly generated app password instead.

SendEmail.java

package com.mkyong; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class SendEmail { public static void main(String[] args) { final String username = “username@gmail.com”; final String password = “puts your app password here”; // update here // same code… } }

java.net.UnknownHostException: smtp.gmail.com
Make sure firewall or proxy server didn’t block this smtp.gmail.com

References

Source

Leave a Reply

Your email address will not be published. Required fields are marked *