In this Java Tutorial we will see how to send an email using GMail SMTP
protocol in Java. I’m using JavaMail API v1.6.2. It is very robust solution available in the market.
The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with Java SE platform and is also included in the Java EE platform. The JavaMail 1.6.2
release contains several bug fixes and enhancements.
Are you getting this error?
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger at javax.mail.Session.initLogger(Session.java:227) at javax.mail.Session.<init>(Session.java:212) at javax.mail.Session.getDefaultInstance(Session.java:315) ... ...
com.sun.mail.util.MailLogger
is part of JavaMail API. It is already included in Enterprise Edition
environment (EE), but it is not included in Standard Edition
environment (SE).
If you are running your test, tests in SE environment which means what you have to bother about adding it manually to your classpath when running tests.
Solution:
If you have maven project then try adding below two dependencies to pom.xml
file to avoid below error:
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.5.0-b01</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.6.2</version> </dependency>
If you want to convert your project to Maven project to have pom.xml file then follow the tutorial.
Java Example:
package crunchify.com.tutorial; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * @author Crunchify.com * */ public class CrunchifyJavaMailExample { static Properties mailServerProperties; static Session getMailSession; static MimeMessage generateMailMessage; public static void main(String args[]) throws AddressException, MessagingException { generateAndSendEmail(); System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email.."); } public static void generateAndSendEmail() throws AddressException, MessagingException { // Step1 System.out.println("\n 1st ===> setup Mail Server Properties.."); mailServerProperties = System.getProperties(); mailServerProperties.put("mail.smtp.port", "587"); mailServerProperties.put("mail.smtp.auth", "true"); mailServerProperties.put("mail.smtp.starttls.enable", "true"); System.out.println("Mail Server Properties have been setup successfully.."); // Step2 System.out.println("\n\n 2nd ===> get Mail Session.."); getMailSession = Session.getDefaultInstance(mailServerProperties, null); generateMailMessage = new MimeMessage(getMailSession); generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("test1@crunchify.com")); generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("test2@crunchify.com")); generateMailMessage.setSubject("Greetings from Crunchify.."); String emailBody = "Test email by Crunchify.com JavaMail API example. " + "<br><br> Regards, <br>Crunchify Admin"; generateMailMessage.setContent(emailBody, "text/html"); System.out.println("Mail Session has been created successfully.."); // Step3 System.out.println("\n\n 3rd ===> Get Session and Send mail"); Transport transport = getMailSession.getTransport("smtp"); // Enter your correct gmail UserID and Password // if you have 2FA enabled then provide App Specific Password transport.connect("smtp.gmail.com", "<----- Your GMAIL ID ----->", "<----- Your GMAIL PASSWORD ----->"); transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients()); transport.close(); } }
Output:
1st ===> setup Mail Server Properties.. Mail Server Properties have been setup successfully.. 2nd ===> get Mail Session.. Mail Session has been created successfully.. 3rd ===> Get Session and Send mail ===> Your Java Program has just sent an Email successfully. Check your email..
Email Sample:
Getting error? How to triage an issue?
- If you’ve turned on
2-Step Verification
for your account, you might need to enter an App password. Important
: If you’re still having problems, visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If necessary, enter the letters in the distorted picture.