Send an email through Gmail in Java
This article shows how to send an email through Gmail in Java.
We use the JavaMail API to send email. You can download the JavaMail API jar from here and the reference implementation jar from here. Or if you are using maven then just add the following dependencies in your pom.xml file.
JavaMail dependencies for Maven
1 2 3 4 5 6 7 8 9 10 11 12 |
<!-- JavaMail dependency --> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.3</version> </dependency> |
We first define the SMTP properties required to connect to the SMTP server. Then, we define an SMTP user authenticator that authenticates the sender’s email and password. We, then, create a mail session and construct the email message represented by MimeMessage object. We, then, use the Transport.send() to send the constructed email object.
You can use the example below to send email through any SMTP host and not just Gmail; only thing that would change are the SMTP properties values as applicable to the target SMTP server. Here, we define the properties required to connect to Gmail’s SMTP server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.wilddiary.mail; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class EmailSender { private static final String SENDERS_EMAIL = "sender@gmail.com"; private static final String SENDERS_PWD = "password"; private static final String RECIPIENTS_EMAIL = "recipient@yahoo.com"; public static void main(String[] args) { Properties mailProps = new Properties(); // Set properties required to connect to Gmail's SMTP server mailProps.put("mail.smtp.host", "smtp.gmail.com"); mailProps.put("mail.smtp.port", "587"); mailProps.put("mail.smtp.auth", "true"); mailProps.put("mail.smtp.starttls.enable", "true"); // Create a username-password authenticator to authenticate SMTP session Authenticator authenticator = new Authenticator() { //override the getPasswordAuthentication method protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(SENDERS_EMAIL, SENDERS_PWD); } }; // Create the mail session Session session = Session.getDefaultInstance(mailProps, authenticator); try{ // Create a default MimeMessage object. final MimeMessage message = new MimeMessage(session); // Set the sender's email address message.setFrom(new InternetAddress(SENDERS_EMAIL)); // Set recipient's email address message.addRecipient(Message.RecipientType.TO, new InternetAddress(RECIPIENTS_EMAIL)); // Set the subject of the email message.setSubject("Hello!"); // Now set the actual message body of the email message.setText("This email was sent using JavaMail API through Gmail! Isn't it awesome?"); System.out.println("Sending email..."); // Send message Transport.send(message); System.out.println("Email sent!"); }catch(Exception e){ System.err.println("Problem sending email. Exception : " + e.getMessage()); } } } |
Output
1 2 3 |
Sending email... Email sent! |
Hey,
I’m getting below error on executing the above program.
Sending email…
Problem sending email. Exception : Send failure (javax.mail.AuthenticationFailedException: 454 4.7.0 Too many login attempts, please try again later. k9sm66764999pga.40 – gsmtp
)
Process finished with exit code 0