Home Javamail Sending Email with Attachment
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Sending Email with Attachment

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

SendAttachment.java

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 = "192.168.10.205";
  String from = "test@localhost";
  String to = "komal@localhost";

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

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

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

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

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

  // Fill the message
  messageBodyPart.setText("hi");

  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(messageBodyPart);

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

  // Put parts in message
  message.setContent(multipart);

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

  
}
}

Output:

MSg Send ....

Download code

Advertisement

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

Ask Questions?    Discuss: Sending Email with Attachment   View All Comments

Post your Comment


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

can anybody please tell me that how test@localhost created????
gmakungo
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.
Raju
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
Ravikumar.N
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.
DMCA.com