<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>The JavaMail API consists of some interfaces and classes used to send, read, and delete e-mail messages. Though there are many packages in the JavaMail API, will cover the main two packages that are used in Java Mail API frequently: <i>javax.mail </i>and <i>javax.mail.internet</i> package. These packages contain all the JavaMail core classes.</p>
<h2>JavaMail API Core Classes</h2>
<p><b>There are two packages that are used in Java Mail API:</b> <i>javax.mail</i> and <i>javax.mail.internet </i>package. These packages contains many classes for Java Mail API. They are:</p>
<ul>
<li>j<i>avax.mail.Session class</i></li>
<li><i>javax.mail.Message class</i></li>
<li><i>javax.mail.internet.MimeMessage class</i></li>
<li><i>javax.mail.Address class</i></li>
<li><i>javax.mail.internet.InternetAddress class</i></li>
<li><i>javax.mail.Authenticator class</i></li>
<li><i>javax.mail.PasswordAuthentication class</i></li>
<li><i>javax.mail.Transport class</i></li>
<li><i>javax.mail.Store class</i></li>
<li><i>javax.mail.Folder class etc.</i></li>
</ul>
<h2><b>Session Class-</b></h2>
<p>The Session class is the primary class of the JavaMail API and it is not subclassed. The Session object acts as the connection factory for the JavaMail API, which handles both configuration setting and authentication.</p>
<p>Session object can be created in the following ways:</p>
<p>1. By looking up the administered object stored in the JNDI service</p>
<pre class="highlight">InitialContext ctx = new InitialContext(); 
Session session = (Session) ctx.lookup("usersMailSession"); 
</pre>
<p>usersMailSession is the JNDI name object used as the administered object for the Session object. usersMailSession can be created and configured with the required parameters as name/value pairs, including information such as the mail server hostname, the user account sending the mail, and the protocols supported by the Session object.</p>
<p>2. Another method of creating the Session object is based on the programmatic approach in which you can use a java.util.Properties object to override some of the default information, such as the mail server name, username, password, and other information that can be shared across your entire application.</p>
<p>The constructor for Session class is private. Hence the Session class provides two methods (listed below) which get the Session object.<br />
<b><br />
</b> <b>3. getDefaultInstance():</b> 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>4. getInstance(): </b>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>
<h2><b>Message Class</b></h2>
<p>With Session object created we now move on to creating a message that will be sent. The message type will be <i><b>javax.mail.Message.</b></i></p>
<p>1. <i>Message is an abstract class.</i> Hence its subclass javax.mail.internet.MimeMessage class is mostly used.</p>
<p>2. To create the message, you need to pass session object in <b><i>MimeMessage </i></b>class constructor.<br />
<b>For example:</b></p>
<pre class="highlight">MimeMessage message=new MimeMessage(session); 
</pre>
<p>3. Once the message object is created we need to store information in it. Message class implements the <i><b>javax.mail.Part</b></i> interface while<b><i> javax.mail.internet. </i><i>MimeMessage </i></b>implements <i><b>javax.mail.internet.MimePart</b></i>. You can either use <b><i>message.setContent() or mimeMessage.setText()</i></b> to store the content.</p>
<p>4. Commonly used methods of <i><b>MimeMessage </b></i>class are</p>
<ul style="text-align: left;">
<li><b>public void setFrom(Address address) </b>used to set the from header field.</li>
<li><b>public void addRecipients(Message.RecipientType type, String addresses)</b> used to add the given address to the recipient type.</li>
<li><b>public void setSubject(String subject)</b> used to set the subject header field.</li>
<li><b>public void setText(String textmessage)</b> used to set the text as the message content using text/plain MIME type.</li>
</ul>
<h2><b>Address Class</b></h2>
<p>Now that we have a <b>Session </b>and <b>Message </b>(with content stored in it) objects, we need to address the letter by using <b>Address </b>object.</p>
<p><i><b>Address </b></i>is an abstract class. Hence its subclass <b><i>javax.mail.internet.InternetAddress </i></b>class is mostly used.</p>
<p>1. Address can be created by just passing email address:</p>
<pre class="highlight">Address address = new InternetAddress("admin@gmail.com"); 
</pre>
<p>2. Another way of creating Address is by passing name alogwith the email address:</p>
<pre class="highlight">Address address = new InternetAddress("admin@gmail.com", admin); 
</pre>
<p>3. You can also set the To, From, CC, BCC fields as below</p>
<pre class="highlight">message.setFrom(address) 
message.addRecipient(type, address) 
</pre>
<p>4. Three predefined address types are objects with one of these values:</p>
<pre class="highlight">Message.RecipientType.TO 
Message.RecipientType.CC 
Message.RecipientType.BCC 
</pre>
<h2><b>Authenticator Class</b></h2>
<p>The class Authenticator represents an object that knows how to obtain authentication for a network connection. Usually, it will do this by prompting the user for information.</p>
<p>Authenticator is an abstract class. You create a subclass <b><i>PasswordAuthentication</i></b>, passing a username and password to its constructor.</p>
<p>You must register the Authenticator with the Session when you create session object.</p>
<p>Following is an example of Authenticator use:</p>
<pre class="highlight">Properties props = new Properties(); 
//Override props with any customized data 
PasswordAuthentication auth = new PasswordAuthentication("admin", "root") 
Session session = Session.getDefaultInstance(props, auth); 
</pre>
<h2><b>Transport Class</b></h2>
<p>Transport class is used as a message transport mechanism. This class normally uses the SMTP protocol to send a message.</p>
<p>1. It is an abstract class.</p>
<p>2. You can use the default version of the class by just calling the static send() method:</p>
<pre class="highlight">Transport.send(message); 
</pre>
<p>3. The other way to send message is by getting a specific instance from the session for your protocol, pass along the username and password (blank if unnecessary), send the message, and close the connection:</p>
<pre class="highlight">message.saveChanges(); // implicit with send() 
//Get transport for session 
Transport transport = session.getTransport("smtp"); 
//Connect 
transport.connect(host, username, password); 
//repeat if necessary 
transport.sendMessage(message, message.getAllRecipients()); 
//Done, close the connection 
transport.close(); 
</pre>
<h2><b>Store Class</b></h2>
<p>An abstract class that models a message store and its access protocol, for storing and retrieving messages. Subclasses provide actual implementations. Store extends the Service class, which provides many common methods for naming stores, connecting to stores, and listening to connection events.</p>
<p>Clients gain access to a Message Store by obtaining a Store object that implements the database access protocol. Most message stores require the user to be authenticated before they allow access. The connect method performs that authentication.</p>
<pre class="highlight">Store store = session.getStore("pop3"); 
store.connect(host, username, password); 
</pre>
<h2><b>Folder Class</b></h2>
<p>Folder is an abstract class that represents a folder for mail messages. Subclasses implement protocol specific Folders. Folders can contain subfolders as well as messages, thus providing a hierarchical structure.</p>
<p>After connecting to the Store, you can then get a Folder, which must be opened before you can read messages from it.</p>
<pre class="highlight">Folder folder = store.getFolder("INBOX"); 
folder.open(Folder.READ_ONLY); 
Message message[] = folder.getMessages(); 
</pre>
<p>The getFolder(String name) method for a Folder object returns the named subfolder. Close the both the Store and Folder connection once reading mail is done.</p>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/javamail-api-environment-setup/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-mail-api-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/sending-email-javamail-api/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/javamail-api-environment-setup/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/sending-email-javamail-api/">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: '249'});
});
</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…