mail

Send email with attachment

With this tutorial we are going to see how to send an email with an attachment in a Java Application. This is particularly useful when you want to handle email activities inside your application.

Basically, all you have to do to send email with attachment is:

  • Set SMTP properties using a Properties object.
  • Use properties.put("mail.smtp.host", "smtp.javacodegeeks.com") to set the smtp host.
  • Use properties.put("mail.smtp.port", "25") to set the smtp port.
  • Create a new Session with the above properties using Session.getDefaultInstance(properties, null).
  • Create a new MimeMessage object with the above session.
  • Use setFrom(new InternetAddress(from)) to set the sender address.
  • Use setRecipient(Message.RecipientType.TO, new InternetAddress(to)) to set the recipient.
  • Use setSubject to set the subject of the message.
  • Create a new MimeBodyPart and use setTex to set the body of the email.
  • Create a new MimeBodyPart for the attachement.
  • Create a new FileDataSource.
  • Use setDataHandler(new DataHandler(fileDataSource)) to set the data handler for the attachment.
  • Add all parts of the email to Multipart object.

Let’s see the code:

package com.javacodegeeks.snippets.enterprise;

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 EmailAttachmentExample {

    public static void main(String[] args) {

  EmailAttachmentExample example = new EmailAttachmentDemo();

  example.sendEmail();

    }

    public void sendEmail() {

  // Strings that contain from, to, subject, body and file path to the attachment

  String from = "sender@javacodegeeks.com";

  String to = "receiver@javacodegeeks.com";

  String subject = "Test mail";

  String body = "Test body";

  String filename = "C:\sample.txt";

  // Set smtp properties

  Properties properties = new Properties();

  properties.put("mail.smtp.host", "smtp.javacodegeeks.com");

  properties.put("mail.smtp.port", "25");

  Session session = Session.getDefaultInstance(properties, null);

  try {

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

message.setSubject(subject);

message.setSentDate(new Date());

// Set the email body

MimeBodyPart messagePart = new MimeBodyPart();

messagePart.setText(body);

// Set the email attachment file

MimeBodyPart attachmentPart = new MimeBodyPart();

FileDataSource fileDataSource = new FileDataSource(filename) {

    @Override

    public String getContentType() {

  return "application/octet-stream";

    }

};

attachmentPart.setDataHandler(new DataHandler(fileDataSource));

attachmentPart.setFileName(fileDataSource.getName());

// Add all parts of the email to Multipart object

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messagePart);

multipart.addBodyPart(attachmentPart);

message.setContent(multipart);

// Send email

Transport.send(message);

  } catch (MessagingException e) {

e.printStackTrace();

  }
    }
}

 
This was an example on how to send email with attachment.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button