java mail api

How to Send email using Java

A user can send email using java with the help of JavaMail API and Java Activation Framework (JAF) installed on the computer/laptop device. Once the user downloads both the latest versions of these files, he can unzip them and install them on the machine. Out of all the available files after unzipping, there are two major files that need to be present in ClassPath which is mail.jar and activation.jar respectively.

Following assumptions have to be made before executing this process:

  • The local host is connected properly to the internet.
  • The internet is capable of sending an email.

There are few parameters also that will be used in the java code to send an email. The user needs to have knowledge about them before writing the code. The parameters are:

  • type: The type parameters defines to whom the email is being addresses. The column TO, CC, BCC will come under type parameter. Hence, type will tell about the message recipient.
  • address: The address parameter is referred to the array of an email ID. There is a method called InternetAddress() for specifying email Ids.

JavaMail API

The main role of JavaMail API is to provide a model which gives access to the classes in the mail system. It is available under javax.mail package. It provides platform-independent and easy sending and receiving emails via java. The classes under this package are based on standards such as SMTP, POP3, IMAP etc.

Java API supports some properties which are always set as a string. The properties are mail. “debug” where the property is set to initial debug mode. Property such as mail. “from” in which the email address of the recipient is returned as the output. Another property such as mail. “host” where the default host property is set. The protocols under the JavaMail API are:

  1. SMTP: It is defined as Simple Mail Transfer Protocol in java language. The main work of this protocol to deliver the email. The following Apache James server, Postcast server etc comes under SMTP. Whenever SMTP is used as the hosting provider, the user will always need authentication for sending and receiving emails.
  2. POP3: It can be termed as Post Office Control in java. The main work of this protocol to receive the email. The facility it provides with is it has a separate mailbox for each activity of the user. Servers such as the Apache James server can also be used for pop, otherwise, the host provides it by default.
  3. IMAP: It is defined as the Internet Message Access Protocol. It is the most advanced and latest version for receiving emails. It has a facility of multiple mailboxes for each user.
  4. MIME: It can be termed as Multiple Internet Mine Extension which takes care of the browser under which the email is being sent.  As a result, this is also called a transfer protocol.

How to Send Email using Java Program

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;  
import javax.mail.internet.*;  
public class SendMailBySite {  
public static void main(String[] args) throws UnsupportedEncodingException {  
String host="";  //Server or Host Name
final String user="";//change accordingly  
final String password="abcdef";//change accordingly  
String to[]= {""};  // Email Address of Reciever
Properties props = new Properties();  
props.put("mail.smtp.auth", "true");  
//Compose the message  
try { 
Session session = Session.getDefaultInstance(props,  
new javax.mail.Authenticator() {  
protected PasswordAuthentication getPasswordAuthentication() {  
return new PasswordAuthentication(user,password);  
for(int s=0;s<to.length;s++) {    
MimeMessage message = new MimeMessage(session);  
message.setFrom(new InternetAddress(user,"Email Name"));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to[s])); 
message.setSubject("Subject of The Email");  
message.setContent("<html><head></head><body>Hi Everyone!!!</body></html>","text/html"); // Write HTML Content which youwnt  to send
//send the message 
System.out.println("message sent successfully to "+s+".) "+to[s]); 
catch (MessagingException e) {e.printStackTrace();}  

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!