<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>Here is an example to send a simple email. Here we have used SMPT server via which emails are sent to our destination email address. There are various ways to send email using JavaMail API. For this purpose, you must have SMTP server that is responsible to send mails.</p>
<h2>Sending Email JavaMail API</h2>
<p>You can use one of the following techniques to get the SMTP server:</p>
<ul style="text-align: left;">
<li>Install and use any SMTP server such as Postcast server, Apache James server, gmail server etc. (or)</li>
<li>Use the SMTP server provided by the host provider e.g. my SMTP server is mail.dineshonjava.com (or)</li>
<li>Use the SMTP Server provided by other companies e.g. gmail etc.</li>
</ul>
<p>Here, we are going to learn above three approaches to send email using javamail API. But we should learn the basic steps to send email from java application.</p>
<p>To send a simple email steps followed are:</p>
<ol style="text-align: left;">
<li>Get the session object that stores all the information of host like host name, username, password etc.</li>
<li>compose the message</li>
<li>send the message</li>
</ol>
<h3><b>Get the session object</b></h3>
<p>The <b><i>javax.mail.Session</i></b> class provides two methods to get the object of session, <i><b>Session.getDefaultInstance()</b></i> method and <b><i>Session.getInstance() </i></b>method. You can use anyone method to get the session object.</p>
<p><b>Examples of these methods are as follows:</b></p>
<p><b><i>1.Syntax of getDefaultInstance() method</i></b><br />
There are two methods to get the session object by using the getDefaultInstance() method. It returns the default session.</p>
<pre class="highlight">public static Session getDefaultInstance(Properties props) 
public static Session getDefaultInstance(Properties props,Authenticator auth) 
</pre>
<p><b><i>Example of getDefaultInstance() method</i></b></p>
<pre class="highlight">Properties properties=new Properties(); 
//fill all the informations like host name etc. 
Session session=Session.getDefaultInstance(properties,null); 
</pre>
<p><i><b>2.Syntax of getInstance() method</b></i><br />
There are two methods to get the session object by using the getInstance() method. It returns the new session.</p>
<pre class="highlight">public static Session getInstance(Properties props) 
public static Session getInstance(Properties props,Authenticator auth) 
</pre>
<p><b><i>Example of getDefaultInstance() method</i></b></p>
<pre class="highlight">Properties properties=new Properties(); 
//fill all the informations like host name etc. 
Session session=Session.getInstance(properties,null); 
</pre>
<h3><b>Compose the message</b></h3>
<p>The <b><i>javax.mail.Message</i></b> class provides methods to compose the message. But it is an abstract class so its subclass <b><i>javax.mail.internet.MimeMessage</i></b> class is mostly used.</p>
<p>To create the message, you need to pass session object in <b><i>MimeMessage </i></b>class constructor.</p>
<p><b>For example:</b></p>
<pre class="highlight">MimeMessage message=new MimeMessage(session); 
</pre>
<p>Now message object has been created but to store information in this object <i><b>MimeMessage </b></i>class provides many methods. Let&#8217;s see methods provided by the <b><i>MimeMessage </i></b>class:</p>
<p><b>Commonly used methods of <i>MimeMessage </i>class</b></p>
<ul style="text-align: left;">
<li><b>public void setFrom(Address address): </b>is used to set the from header field.</li>
<li><b>public void addRecipients(Message.RecipientType type, String addresses): </b>is used to add the given address to the recipient type.</li>
<li><b>public void setSubject(String subject):</b> is used to set the subject header field.</li>
<li><b>public void setText(String textmessage)</b> is used to set the text as the message content using text/plain MIME type.</li>
</ul>
<p><b>Example to compose the message:</b></p>
<pre class="highlight">MimeMessage message=new MimeMessage(session); 
message.setFrom(new InternetAddress("dineshonjava@gmail.com")); 
message.addRecipient(Message.RecipientType.To, 
new InternetAddress("admin@dineshonjava.com")); 
message.setHeader("Wecome to JAVAMail Tutorial"); 
message.setText("Hi Users, This is java mail tutorial..."); 
</pre>
<h3><b>Send the message</b></h3>
<p>The <i><b>javax.mail.Transport</b></i> class provides method to send the message.</p>
<p><b>Commonly used methods of Transport class</b></p>
<ul style="text-align: left;">
<li><b>public static void send(Message message):</b> is used send the message.</li>
<li><b>public static void send(Message message, Address[] address):</b> is used send the message to the given addresses.</li>
</ul>
<p><b>Example to send the message:</b></p>
<pre class="highlight">Transport.send(message); 
</pre>
<p><b>Sending Simple Email Example-</b></p>
<h2>Create Java Class</h2>
<p>Create a java class file <b>SendEmail</b>, the contents of which are as follows:</p>
<p><b>JavaMail – GMail via TLS</b></p>
<p>Send an Email via Gmail SMTP server using TLS connection.</p>
<pre class="highlight">import java.util.Properties; 
 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
 
public class SendEmail { 
 public static void main(String[] args) { 
 // Recipient's email ID needs to be mentioned. 
 String to = "dinesh.mca.jss@gmail.com"; 
 
 // Sender's email ID needs to be mentioned 
 String from = "dineshonjava@gmail.com"; 
 final String username = "dineshonjava";//change accordingly 
 final String password = "*****";//change accordingly 
 
 // Assuming you are sending email through relay.jangosmtp.net 
 String host = "smtp.gmail.com"; 
 
 Properties props = new Properties(); 
 props.put("mail.smtp.auth", "true"); 
 props.put("mail.smtp.starttls.enable", "true"); 
 props.put("mail.smtp.host", "smtp.gmail.com"); 
 props.put("mail.smtp.port", "587"); 
 
 // Get the Session object. 
 Session session = Session.getInstance(props, 
 new javax.mail.Authenticator() { 
 protected PasswordAuthentication getPasswordAuthentication() { 
 return new PasswordAuthentication(username, password); 
 } 
 }); 
 
 try { 
 // Create a default MimeMessage object. 
 Message message = new MimeMessage(session); 
 
 // Set From: header field of the header. 
 message.setFrom(new InternetAddress(from)); 
 
 // Set To: header field of the header. 
 message.setRecipients(Message.RecipientType.TO, 
 InternetAddress.parse(to)); 
 
 // Set Subject: header field 
 message.setSubject("Testing Subject"); 
 
 // Now set the actual message 
 message.setText("Hello, this is sample for to check send " + 
 "email using JavaMailAPI "); 
 
 // Send message 
 Transport.send(message); 
 
 System.out.println("Sent message successfully...."); 
 
 } catch (MessagingException e) { 
 throw new RuntimeException(e); 
 } 
 } 
} 
</pre>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/01/1111-17.png" border="0" /></div>
<p>For sending the email using <b>JavaMail </b>API, you need to load the two jar files:</p>
<ul style="text-align: left;">
<li>mail.jar</li>
<li>activation.jar</li>
</ul>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/01/1111-18.png" border="0" /></div>
<p> ;</p>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/javamail-api-core-classes/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-mail-api-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/send-email-via-gmail-smtp/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/javamail-api-core-classes/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/send-email-via-gmail-smtp/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '248'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…