In this article, you will learn How to Send Email in Java Programming Language. It will cover sending simple emails, html emails and sending emails with attachments.
To perform all the above tasks you will need the following software installed on your computer.
- JavaMail API
- Java Activation Framework(JAF)
- Access to SMTP Server
- A valid SMTP Server Username and Password
The following two jar files should be added in your classpath in order to send and receive emails using JavaMail API.
- mail.jar
- activation.jar
These jar files are loaded in the following way.
c:\> set classpath=mail.jar;activation.jar;.;
SENDING A SIMPLE EMAIL
The following steps are involved to send a simple email in java programming language.
Step1: Getting Session Object
To get the session object, javax.mail.Session class is used which contains the following methods:
- public static Session getDefaultInstance(Properties ps)
- public static Session getDefaultInstance(Properties ps, Authenticator a)
- public static Session getInstance(Properties ps)
- public static Session getInstance(Properties ps, Authenticator a)
Step2: Composing Message
To compose a message, javax.mail.Message class is used which contains the following methods:
- public void setFrom(Address address)
- public void addRecipient(Message.RecipientType type, Address address)
- public void setSubject(String subject)
- public void setText(String textmessage)
- public void setContent(Object msg, String contentType)
- public void addRecipients(Message.RecipientType type, Address[] addresses)
Step3: Sending Message
To send the message, javax.mail.Transport class is used which contains the following methods:
- public static void send(Message message)
- public static void send(Message message, Address[] address)
Example:
//Filename: SendingEmailEx.java
import java.util.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
class SendingEmailEx {
public static void main(String [] args) {
String to = “recipient@gmail.com” ;
String from = “sender@gmail.com” ;
String host = “localhost” ;
//Getting Session Object
Properties properties = System.getProperties() ;
properties.setProperty(“mail.smtp.host”, host) ;
Session session = Session.getDefaultInstance(properties) ;
//Composing Message
try {
MimeMessage msg = new MimeMessage(session) ;
msg.setFrom(new InternetAddress(from)) ;
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)) ;
msg.setSubject(“Subject goes here”) ;
msg.setText(“Message goes here”) ;
//Sending Message
Transport.send(msg) ;
System.out.println(“Your Message Was Sent Successfully”) ;
}
catch (MessagingException ref) {
ref.printStackTrace() ;
}
}
}
Compiling:
c:\> javac SendingEmailEx.java
Running:
c:\> java SendingEmailEx
Output:
Your Message Was Sent Successfully
SENDING AN HTML EMAIL
To send an HTML email, setContent() method is used which contains HTML contents. Sending an HTML email is similar to the way we send a simple email except the inclusion of setContent() method.
Example:
//Filename: SendingEmailEx.java
import java.util.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
class SendingHTMLEmailEx {
public static void main(String [] args) {
String to = “recipient@gmail.com” ;
String from = “sender@gmail.com” ;
String host = “localhost” ;
//Getting Session Object
Properties properties = System.getProperties() ;
properties.setProperty(“mail.smtp.host”, host) ;
Session session = Session.getDefaultInstance(properties) ;
//Composing Message
try {
MimeMessage msg = new MimeMessage(session) ;
msg.setFrom(new InternetAddress(from)) ;
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)) ;
msg.setSubject(“Subject goes here”) ;
msg.setContent(“<h1>Message goes here</h1>”, “text/html”) ;
//Sending Message
Transport.send(msg) ;
System.out.println(“Your Message Was Sent Successfully”) ;
}
catch (MessagingException ref) {
ref.printStackTrace() ;
}
}
}
Compiling:
c:\> javac SendingHTMLEmailEx.java
Running:
c:\> java SendingHTMLEmailEx
Output:
Your Message Was Sent Successfully
SENDING EMAIL WITH AN ATTACHMENT
To send an email with an attachment, BodyPart, MimeBodyPart etc., classes are used provided by the JavaMail API. We have shown an example of attaching a file with an email and sending it to the recipient using java programming language.
Example:
// Filename: SendingAttachment.java
import java.util.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
class SendingAttachment {
public static void main(String [] args) {
String to = “recipient@gmail.com” ;
String from = “sender@gmail.com” ;
String host = “localhost” ;
//Getting Session Object
Properties properties = System.getProperties() ;
properties.setProperty(“mail.smtp.host”, host) ;
Session session = Session.getDefaultInstance(properties) ;
//Composing Message
try {
MimeMessage msg = new MimeMessage(session) ;
msg.setFrom(new InternetAddress(from)) ;
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)) ;
msg.setSubject(“Subject goes here”) ;
//Creating Message Part
BodyPart msgBodyPart = new MimeBodyPart() ;
msgBodyPart.setText(“Message goes here”) ;
Multipart multipart = new MimeMultipart() ;
multipart.addBodyPart(msgBodyPart) ;
//Creating Attachment Part
msgBodyPart = new MimeBodyPart() ;
String file = “file.txt” ;
DataSource src = new FileDataSource(file) ;
msgBodyPart.setDataHandler(new DataHandler(src)) ;
msgBodyPart.setFileName(file) ;
multipart.addBodyPart(msgBodyPart) ;
//Sending Both the Message and Attachment Parts
msg.setContent(multipart) ;
//Sending Message
Transport.send(msg) ;
System.out.println(“Your Message Was Sent Successfully”) ;
}
catch (MessagingException ref) {
ref.printStackTrace() ;
}
}
}
Compiling:
c:\> javac SendingAttachment.java
Running:
c:\> java SendingAttachment
Output:
Your Message Was Sent Successfully
Also Read:- Generic Methods and Classes in Java Programming Language
We have provided you the description on How to Send a Simple Email, HTML Email, Email with Attachments in Java Programming Language. Hope you like this article. For more updates and related information, stay connected to our blogs on Java Programming Language.