How to Send an Email Message Using Java Mail API

There can be many situations when the need of sending email to the user/client arises while using the application for example when a user registers on a site, generally a confirmation mail or a password verification mail is sent to him.

So it is very common while performing an e-commerce based transaction to send or receive data through emails. In a J2EE based web application Java Mail API is an easy method to send and receive email. Here we will discuss the procedure of sending email using Java Mail API.

For accomplishing this we need Java Activation Framework(JAF) and JavaMail API to be downloaded on our system. There are three basic steps to be performed for sending the mail. First of all the j2ee component which we are using to do the task( eg. Servlet) needs to create a session. Then next step should be of developing the email message.

There are different protocols which define the standard way for formatting an email message. For example we have MIME, SMTP or POP3. Each of these have different features and capabilities of formatting and transport of email message. Client and dedicated servers can use any of the protocol and JavaMail API needs to interact with these protocols for performing its task. After creating the email, third step is to send the email.

Consider the following example for understanding this three step process in detail:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class SendingMail
public static void main(String args[])
String myhost= “”;
String frm= “[email protected]”;
String to= [email protected];
Properties prp = System.getProperties();
prp.put( “mail.smtp.myhost”,myhost);
Session sess1=Session.getDefaultInstance(prp,null);
MimeMessage mesg= new MimeMessage(sess1);
mesg.setFrom(new InternetAddress(frm));
mesg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
mesg.setSubject(“regarding your interview”);
mesg.setText(“you are selected for job”);

In the above described example, we have described three String objects: frm,to and myhost. frm is assigned the email address of sender and to has the recipient’s mail address. myhost is assigned the smtp host. Here we are assuming that email formatting is done by using MIME (Multipurpose Internet Mail Extension), so MimeMessage object is created. put() method is used to set the smtp host. After setting the host, session is created using getDefaultInstance() method which is referred as sess1. As clear from the example MimeMessage object is referred as mesg.

After this ‘frm’ and ‘to’ objects are converted to InternetAddress objects from String objects. Then we have used setFrom() and addRecipient() methods where these frm and to objects are passed as arguments. This is done in order to set the email address. After this we have used setSubject() method for passing the subject line of message and then setText() method to set the email body. Finally send () method of Transport object is used to send the email message.


SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.


| All About Different Types of XML Editors | Generating XML Document Using JSP | How to Retrieve an Email Message Using Java Mail API | How to Send an Email Message Using Java Mail API | Transforming XML Data with XSLT | Understanding Basic Components of a JMS Program | Understanding Basic Technology of ebXML | Understanding Deployment Descriptor in Reference with Servlets | Understanding eXtensible Access Control Markup Language (XACML) | Understanding the Design Goals of XML | Understanding XML Common Biometric Format |

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.



Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.



Add to My Yahoo!

Visit XML Training Material Guide Homepage




“Amazon and the Amazon logo are trademarks of, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.