How do I send an email with attachment?

In this example we create a small program to send email with a file attachment. To send message with attachment we need to create an email with javax.mail.Multipart object which basically will contains the email text message and then add a file to the second block, which both of them is an object of javax.mail.internet.MimeBodyPart. In this example we also use the javax.activation.FileDataSource.

package org.kodejava.example.mail;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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.Date;
import java.util.Properties;

public class SendEmailWithAttachment {
    public static void main(String[] args) {
        SendEmailWithAttachment demo = new SendEmailWithAttachment();

    public void sendEmail() {
        // Defines the E-Mail information.
        String from = "[email protected]";
        String to = "[email protected]";
        String subject = "Important Message";
        String bodyText = "This is a important message with attachment.";

        // The attachment file name.
        String attachmentName = "/Users/wsaryada/Documents/data.pdf";

        // Creates a Session with the following properties.
        Properties props = new Properties();
        props.put("", "");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getDefaultInstance(props);

        try {
            InternetAddress fromAddress = new InternetAddress(from);
            InternetAddress toAddress = new InternetAddress(to);

            // Create an Internet mail message.
            MimeMessage message = new MimeMessage(session);
            message.setRecipient(Message.RecipientType.TO, toAddress);
            message.setSentDate(new Date());

            // Set the email message text.
            MimeBodyPart messagePart = new MimeBodyPart();

            // Set the email attachment file
            FileDataSource fileDataSource = new FileDataSource(attachmentName);

            MimeBodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.setDataHandler(new DataHandler(fileDataSource));

            // Create Multipart E-Mail.
            Multipart multipart = new MimeMultipart();


            // Send the message. Don't forget to set the username and password 
            // to authenticate to the mail server.
            Transport.send(message, "kodejava", "**********");
        } catch (MessagingException e) {

Maven Dependencies


Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.


  1. Hi.. I followed your code its works for me… but i want file name as dynamic i.e user uploaded file as attchment..iam using strust1.x … please help me

Leave a Reply