Categories: JavaMail

Sending Email JavaMail API

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>Here is an example to send a simple email&period; Here we have used SMPT server via which emails are sent to our destination email address&period; There are various ways to send email using JavaMail API&period; For this purpose&comma; you must have SMTP server that is responsible to send mails&period;<&sol;p>&NewLine;<h2>Sending Email JavaMail API<&sol;h2>&NewLine;<p>You can use one of the following techniques to get the SMTP server&colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Install and use any SMTP server such as Postcast server&comma; Apache James server&comma; gmail server etc&period; &lpar;or&rpar;<&sol;li>&NewLine;<li>Use the SMTP server provided by the host provider e&period;g&period; my SMTP server is mail&period;dineshonjava&period;com &lpar;or&rpar;<&sol;li>&NewLine;<li>Use the SMTP Server provided by other companies e&period;g&period; gmail etc&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Here&comma; we are going to learn above three approaches to send email using javamail API&period; But we should learn the basic steps to send email from java application&period;<&sol;p>&NewLine;<p>To send a simple email steps followed are&colon;<&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li>Get the session object that stores all the information of host like host name&comma; username&comma; password etc&period;<&sol;li>&NewLine;<li>compose the message<&sol;li>&NewLine;<li>send the message<&sol;li>&NewLine;<&sol;ol>&NewLine;<h3><b>Get the session object<&sol;b><&sol;h3>&NewLine;<p>The <b><i>javax&period;mail&period;Session<&sol;i><&sol;b> class provides two methods to get the object of session&comma; <i><b>Session&period;getDefaultInstance&lpar;&rpar;<&sol;b><&sol;i> method and <b><i>Session&period;getInstance&lpar;&rpar; <&sol;i><&sol;b>method&period; You can use anyone method to get the session object&period;<&sol;p>&NewLine;<p><b>Examples of these methods are as follows&colon;<&sol;b><&sol;p>&NewLine;<p><b><i>1&period;Syntax of getDefaultInstance&lpar;&rpar; method<&sol;i><&sol;b><br &sol;>&NewLine;There are two methods to get the session object by using the getDefaultInstance&lpar;&rpar; method&period; It returns the default session&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public static Session getDefaultInstance&lpar;Properties props&rpar; &NewLine;public static Session getDefaultInstance&lpar;Properties props&comma;Authenticator auth&rpar; &NewLine;<&sol;pre>&NewLine;<p><b><i>Example of getDefaultInstance&lpar;&rpar; method<&sol;i><&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">Properties properties&equals;new Properties&lpar;&rpar;&semi; &NewLine;&sol;&sol;fill all the informations like host name etc&period; &NewLine;Session session&equals;Session&period;getDefaultInstance&lpar;properties&comma;null&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p><i><b>2&period;Syntax of getInstance&lpar;&rpar; method<&sol;b><&sol;i><br &sol;>&NewLine;There are two methods to get the session object by using the getInstance&lpar;&rpar; method&period; It returns the new session&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public static Session getInstance&lpar;Properties props&rpar; &NewLine;public static Session getInstance&lpar;Properties props&comma;Authenticator auth&rpar; &NewLine;<&sol;pre>&NewLine;<p><b><i>Example of getDefaultInstance&lpar;&rpar; method<&sol;i><&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">Properties properties&equals;new Properties&lpar;&rpar;&semi; &NewLine;&sol;&sol;fill all the informations like host name etc&period; &NewLine;Session session&equals;Session&period;getInstance&lpar;properties&comma;null&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<h3><b>Compose the message<&sol;b><&sol;h3>&NewLine;<p>The <b><i>javax&period;mail&period;Message<&sol;i><&sol;b> class provides methods to compose the message&period; But it is an abstract class so its subclass <b><i>javax&period;mail&period;internet&period;MimeMessage<&sol;i><&sol;b> class is mostly used&period;<&sol;p>&NewLine;<p>To create the message&comma; you need to pass session object in <b><i>MimeMessage <&sol;i><&sol;b>class constructor&period;<&sol;p>&NewLine;<p><b>For example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">MimeMessage message&equals;new MimeMessage&lpar;session&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p>Now message object has been created but to store information in this object <i><b>MimeMessage <&sol;b><&sol;i>class provides many methods&period; Let&&num;8217&semi;s see methods provided by the <b><i>MimeMessage <&sol;i><&sol;b>class&colon;<&sol;p>&NewLine;<p><b>Commonly used methods of <i>MimeMessage <&sol;i>class<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>public void setFrom&lpar;Address address&rpar;&colon; <&sol;b>is used to set the from header field&period;<&sol;li>&NewLine;<li><b>public void addRecipients&lpar;Message&period;RecipientType type&comma; String addresses&rpar;&colon; <&sol;b>is used to add the given address to the recipient type&period;<&sol;li>&NewLine;<li><b>public void setSubject&lpar;String subject&rpar;&colon;<&sol;b> is used to set the subject header field&period;<&sol;li>&NewLine;<li><b>public void setText&lpar;String textmessage&rpar;<&sol;b> is used to set the text as the message content using text&sol;plain MIME type&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>Example to compose the message&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">MimeMessage message&equals;new MimeMessage&lpar;session&rpar;&semi; &NewLine;message&period;setFrom&lpar;new InternetAddress&lpar;"dineshonjava&commat;gmail&period;com"&rpar;&rpar;&semi; &NewLine;message&period;addRecipient&lpar;Message&period;RecipientType&period;To&comma; &NewLine;new InternetAddress&lpar;"admin&commat;dineshonjava&period;com"&rpar;&rpar;&semi; &NewLine;message&period;setHeader&lpar;"Wecome to JAVAMail Tutorial"&rpar;&semi; &NewLine;message&period;setText&lpar;"Hi Users&comma; This is java mail tutorial&period;&period;&period;"&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<h3><b>Send the message<&sol;b><&sol;h3>&NewLine;<p>The <i><b>javax&period;mail&period;Transport<&sol;b><&sol;i> class provides method to send the message&period;<&sol;p>&NewLine;<p><b>Commonly used methods of Transport class<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>public static void send&lpar;Message message&rpar;&colon;<&sol;b> is used send the message&period;<&sol;li>&NewLine;<li><b>public static void send&lpar;Message message&comma; Address&lbrack;&rsqb; address&rpar;&colon;<&sol;b> is used send the message to the given addresses&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>Example to send the message&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">Transport&period;send&lpar;message&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p><b>Sending Simple Email Example-<&sol;b><&sol;p>&NewLine;<h2>Create Java Class<&sol;h2>&NewLine;<p>Create a java class file <b>SendEmail<&sol;b>&comma; the contents of which are as follows&colon;<&sol;p>&NewLine;<p><b>JavaMail – GMail via TLS<&sol;b><&sol;p>&NewLine;<p>Send an Email via Gmail SMTP server using TLS connection&period;<&sol;p>&NewLine;<pre class&equals;"highlight">import java&period;util&period;Properties&semi; &NewLine; &NewLine;import javax&period;mail&period;Message&semi; &NewLine;import javax&period;mail&period;MessagingException&semi; &NewLine;import javax&period;mail&period;PasswordAuthentication&semi; &NewLine;import javax&period;mail&period;Session&semi; &NewLine;import javax&period;mail&period;Transport&semi; &NewLine;import javax&period;mail&period;internet&period;InternetAddress&semi; &NewLine;import javax&period;mail&period;internet&period;MimeMessage&semi; &NewLine; &NewLine;public class SendEmail &lbrace; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; &sol;&sol; Recipient's email ID needs to be mentioned&period; &NewLine; String to &equals; "dinesh&period;mca&period;jss&commat;gmail&period;com"&semi; &NewLine; &NewLine; &sol;&sol; Sender's email ID needs to be mentioned &NewLine; String from &equals; "dineshonjava&commat;gmail&period;com"&semi; &NewLine; final String username &equals; "dineshonjava"&semi;&sol;&sol;change accordingly &NewLine; final String password &equals; "&ast;&ast;&ast;&ast;&ast;"&semi;&sol;&sol;change accordingly &NewLine; &NewLine; &sol;&sol; Assuming you are sending email through relay&period;jangosmtp&period;net &NewLine; String host &equals; "smtp&period;gmail&period;com"&semi; &NewLine; &NewLine; Properties props &equals; new Properties&lpar;&rpar;&semi; &NewLine; props&period;put&lpar;"mail&period;smtp&period;auth"&comma; "true"&rpar;&semi; &NewLine; props&period;put&lpar;"mail&period;smtp&period;starttls&period;enable"&comma; "true"&rpar;&semi; &NewLine; props&period;put&lpar;"mail&period;smtp&period;host"&comma; "smtp&period;gmail&period;com"&rpar;&semi; &NewLine; props&period;put&lpar;"mail&period;smtp&period;port"&comma; "587"&rpar;&semi; &NewLine; &NewLine; &sol;&sol; Get the Session object&period; &NewLine; Session session &equals; Session&period;getInstance&lpar;props&comma; &NewLine; new javax&period;mail&period;Authenticator&lpar;&rpar; &lbrace; &NewLine; protected PasswordAuthentication getPasswordAuthentication&lpar;&rpar; &lbrace; &NewLine; return new PasswordAuthentication&lpar;username&comma; password&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub;&rpar;&semi; &NewLine; &NewLine; try &lbrace; &NewLine; &sol;&sol; Create a default MimeMessage object&period; &NewLine; Message message &equals; new MimeMessage&lpar;session&rpar;&semi; &NewLine; &NewLine; &sol;&sol; Set From&colon; header field of the header&period; &NewLine; message&period;setFrom&lpar;new InternetAddress&lpar;from&rpar;&rpar;&semi; &NewLine; &NewLine; &sol;&sol; Set To&colon; header field of the header&period; &NewLine; message&period;setRecipients&lpar;Message&period;RecipientType&period;TO&comma; &NewLine; InternetAddress&period;parse&lpar;to&rpar;&rpar;&semi; &NewLine; &NewLine; &sol;&sol; Set Subject&colon; header field &NewLine; message&period;setSubject&lpar;"Testing Subject"&rpar;&semi; &NewLine; &NewLine; &sol;&sol; Now set the actual message &NewLine; message&period;setText&lpar;"Hello&comma; this is sample for to check send " &plus; &NewLine; "email using JavaMailAPI "&rpar;&semi; &NewLine; &NewLine; &sol;&sol; Send message &NewLine; Transport&period;send&lpar;message&rpar;&semi; &NewLine; &NewLine; System&period;out&period;println&lpar;"Sent message successfully&period;&period;&period;&period;"&rpar;&semi; &NewLine; &NewLine; &rcub; catch &lpar;MessagingException e&rpar; &lbrace; &NewLine; throw new RuntimeException&lpar;e&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;01&sol;1111-17&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>For sending the email using <b>JavaMail <&sol;b>API&comma; you need to load the two jar files&colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>mail&period;jar<&sol;li>&NewLine;<li>activation&period;jar<&sol;li>&NewLine;<&sol;ul>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;01&sol;1111-18&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;javamail-api-core-classes&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-mail-api-tutorial&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;send-email-via-gmail-smtp&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;javamail-api-core-classes&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;send-email-via-gmail-smtp&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '248'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago