Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Javamail Sending Email with Attachment

Related Tutorials


Share on Google+Share on Google+

Sending Email with Attachment

This Example shows you how to send a Attachment in the message using java mail api.

Sending Email with Attachment


This Example shows you how to send a Attachment in the message using java mail api. A client create new message by using Message subclass. It sets attributes like recipient address and the subject, and inserts the content into the Message object, and inserts the content into the Message object, and add attachment in the content and Finally, it sends the Message by invoking the Transport.send() method.

By this code you can get functionality to send Email with a
attached text file, zip file or a image file etc.

import java.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;

public class SendAttachment {

  public static void main(String args[]) throws Exception {
  String host = "";
  String from = "test@localhost";
  String to = "komal@localhost";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("", host);

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  // Define message
  Message message = new MimeMessage(session);
  message.setFrom(new InternetAddress(from));
  new InternetAddress(to));
  message.setSubject("JavaMail Attachment");

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

  // Fill the message

  Multipart multipart = new MimeMultipart();

  // Part two is attachment
  messageBodyPart = new MimeBodyPart();
  String filename = "file.txt";
  DataSource source = new FileDataSource(filename);
  messageBodyPart.setDataHandler(new DataHandler(source));

  // Put parts in message

  // Send the message
   System.out.println("Msg Send ....")



MSg Send ....

Download code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 13, 2009

Related Tutorials

Discuss: Sending Email with Attachment   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 1, 2011

can anybody please tell me that how test@localhost created????
June 14, 2013
thank you

the code works very well, thank you so much
Kiran Kumar Mandadapu`
June 21, 2012
How to add images in mail attached html reports

hai, I want to add an image into an html report and i want to send the same html as an mail attachment.But in normally if we use Normal attachment,Its works fine in our system.But for receivers it may get some ambiguity to find the location of the image in CID's way we are able to add the images in the mail body only,but its not possible to us ethe same in the attachment. Can any one please help me. Thanks & Regards, Kiran Kumar.
November 27, 2012
Attachment content

Hi am new to java can you please tell what will be the content of the file.txt attachment in this case, is it hi or anything else .please clarify regards raju
June 9, 2011
Sending mail with attachment

Hai Friends, Can you help my project for how to send a mail with pdf attachment using JavaScript.