Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Attaching files using Java Mail API

Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!
I want to create a new log file with extension RTF. This RTF file will contain many messages about what has occured in my program.
Then, using Java Mail, I want to attach this just-created file and send it via SMTP.
How can I accomplish this?
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use a multipart message ,
here is some sample code.

public static void main(String[] args) {
try {
if (args.length != 7) {
System.out.println("usage: java sendfile <to> <from> <subject> <text> <smtp host> <file> true|false");
String to = args[0];
if ((to == null) || (to.trim().equals("")))
throw new Exception("To Address is balnk");
String from = args[1];
if ((from == null) || (from.trim().equals("")))
throw new Exception("From Address is balnk");
String msgText1 = args[2];
String subject = args[3];
String host = args[4];
if ((host == null) || (host.trim().equals("")))
throw new Exception("Host Address is balnk");
String filename = args[5];
boolean debug = Boolean.valueOf(args[6]).booleanValue();
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("", host);
Session session = Session.getDefaultInstance(props, null);
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
if ((filename == null) || (filename.trim().equals(""))) {
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
if ((filename == null) || (filename.trim().equals("")))
// add the Multipart to the message
// set the Date: header
msg.setSentDate(new Date());
// send the message
} catch (MessagingException mex) {
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
} catch (Exception e) {
this is a modiifcation of the sample code within the mail api.
Hope this helps
Sunil Nair
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic