Core Java - SPLessons

Java Sending Email

Chapter 40

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Sending Email

Java Sending Email

shape Introduction

Java Sending Email, JavaMail API is not part of standard JDK, the developer will have to install it from the web site and include it in build path, jar file needs to be imported that is Java.mail.jar. Following is an example to send the documents from one to another.

shape Conceptual

Steps to send an email with attachments:
Java Sending Email, The process for sending mail with attachment involves a session object, MineBody, MultiPart objects. Here the mind-body is used to set the text message and it is carried by MultiPart object.

shape Examples

Let us see the example for the concept Java Sending Email like how to send email with attachments.

[c] package firstmail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class sendingattachments {
public static void main(String[] args) {
// Recipient’s email ID needs to be mentioned.
String to = “”;

// Sender’s email ID needs to be mentioned
String from = “”;

final String username = “”;//change accordingly
final String password = “splessons”;//change accordingly

// Assuming you are sending email through
Properties props = new Properties();
props.put(“”, “”);
props.put(“mail.smtp.socketFactory.port”, “465”);
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.port”, “465”);

// 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.

// Set Subject: header field

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Now set the actual message
messageBodyPart.setText(“hey u, follwong is the message find it.”);

// Create a multipar message
Multipart multipart = new MimeMultipart();

// Set text message part

// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = “E:/splesson-CV.PDF”;
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));

// Send the complete message parts

// Send message

System.out.println(“Sent message successfully….”);

} catch (MessagingException e) {
throw new RuntimeException(e);
Recipient’s email ID needs to be mentioned.
[java]String to = “”;[/java]
Sender’s email ID needs to be mentioned
[java]String from = “”;[/java]
Set the properties to the SMTP server as follows.
[java]props.put(“”, “”);
props.put(“mail.smtp.socketFactory.port”, “465”);
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.port”, “465”);[/java]

Java Sending Email, When compile the program following is the output will be displayed in the console.

[c]Sent message successfully….[/c]
Now check the mail.

Open the mail and find the attachment.


shape Key Points

  • By making use of MineBodyPart, BodyPart classes from JavaMail API sending electronic mail with attachment is possible.
  • The process for sending mail with attachment involves session object, MineBody, MultiPart objects.
  • Here the MineBody is used to set the text message and it is carried by MultiPart object.