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.
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
// Define message
Message message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 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);
// Put parts in message
// Send the message
System.out.println("Msg Send ....")
June 1, 2011
can anybody please tell me that how [email protected] created????
June 14, 2013
the code works very well, thank you so much
Kiran Kumar Mandadapu`
June 21, 2012
How to add images in mail attached html reports
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,
November 27, 2012
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
June 9, 2011
Sending mail with attachment