Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

How to attach a file to a Email in Java using Java Mail API ?.

Program to demonstrate how to attach a file to a Email in Java using Java Mail API


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;
import java.util.Scanner;

public class SendAttachmentEmailDemo {

 public static void main(String[] args) {

  // Create a public Class SendAttachmentEmailDemo object
  // and call sendAttachmentEmail() on it ...
  SendAttachmentEmailDemo sendAttachmentEmailDemo = new SendAttachmentEmailDemo();


 private void sendAttachmentEmail() {

  // For establishment of email client with 
  // Google's gmail use below properties.
  // For TLS Connection use below properties
  // Create a Properties object 
  Properties props = new Properties();

  // these properties are required
  // providing smtp auth property to true
  props.put("mail.smtp.auth", "true");
  // providing tls enability
  props.put("mail.smtp.starttls.enable", "true");
  // providing the smtp host i.e
  props.put("", "");
  // providing smtp port as 587
  props.put("mail.smtp.port", "587");

  // For SSL Connection use below properties

  /*props.put("", "");
   props.put("mail.smtp.socketFactory.port", "465");
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.port", "465");*/

  // Create Scanner object to take necessary 
  // values from the user.
  Scanner scanner = new Scanner(;

  System.out.println("Please provide your Username for Authentication ...");
  final String Username = scanner.nextLine();

  System.out.println("Please provide your Password for Authentication ...");
  final String Password = scanner.nextLine();

  System.out.println("Please provide Email Address from which you want to send Email ...");
  final String fromEmailAddress = scanner.nextLine();

  System.out.println("Please provide Email Address to which you want to send Email ...");
  final String toEmailAddress = scanner.nextLine();

  System.out.println("Please provide Subject for your Email ... ");
  final String subject = scanner.nextLine();

  System.out.println("Please provide Text Message for your Email ... ");
  final String textMessage = scanner.nextLine();

  System.out.println("Please provide the location of file to attach ...");
  final String attachment = scanner.nextLine();

  // Create a Session object based on the properties and 
  // Authenticator object
  Session session = Session.getDefaultInstance(props, 
    new LoginAuthenticator(Username,Password));

  try {

   // Create a Message object using the session created above
   Message message = new MimeMessage(session);

   // setting email address to Message from where message is being sent
   message.setFrom(new InternetAddress(fromEmailAddress));

   // setting the email address to which user wants to send message 

   // setting the subject for the email 

   // Multipart messages contains different formats of files 
   // images and text messages combined together in a email.
   // Such messages are also called as Multipurpose Internet Mail
   // Extensions (MIME) messages

   // Lets create first message body part , which will point
   // to our body of email message. Create a MimeBodyPart
   // object calling its setContent() method to set our 
   // email body
   MimeBodyPart msgBodyPart = new MimeBodyPart();
   msgBodyPart.setContent(textMessage, "text/plain");

   // Create a MimeBodyPart object calling its attachFile()
   // method to enable email to sent attachments such as text files , images etc.

   MimeBodyPart attachmentBodypart = new MimeBodyPart();

   // Create a Multipart object and assign the parts created above to it
   // using the addBodyPart() method.
   Multipart multipart = new MimeMultipart();

   // Setting the content of message to be the 
   // multipart created above

   // Sending the attachment mail by Transport class send() message

   //System.out.println("\nYour Message delivered successfully ....");

  } catch (MessagingException e) {

   throw new RuntimeException(e);

  catch (IOException e) {


 // Creating a class for Username and Password authentication
 // provided by the
 class LoginAuthenticator extends Authenticator {
  PasswordAuthentication authentication = null;

  public LoginAuthenticator(String username, String password) {
   authentication = new PasswordAuthentication(username,password);

  protected PasswordAuthentication getPasswordAuthentication() {
   return authentication;


Output of the program : 

© 2021 Learn Java by Examples Template by Hubberspot