File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Attaching files using Java Mail API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Attaching files using Java Mail API" Watch "Attaching files using Java Mail API" New topic
Author

Attaching files using Java Mail API

Luciano Queiroz
Ranch Hand

Joined: Feb 04, 2002
Posts: 172
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?
Thanks!


<b>Luciano Queiroz</b><br/>
 Brazil<br/>
 <i>SCJP 1.2, SCWCD, IBM 141, IBM 483, SCJP 1.4, IBM 484, IBM 287, SCBCD, SCEA, SCJP 5.0, SCJP 6.0, SCWCD 5</i>
sunil g nair
Greenhorn

Joined: Jun 03, 2002
Posts: 14
Hey,
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");
System.exit(1);
}
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("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// 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));
mbp2.setFileName(fds.getName());
}
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
if ((filename == null) || (filename.trim().equals("")))
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
this is a modiifcation of the sample code within the mail api.
Hope this helps
Thanks,
Sunil Nair
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Attaching files using Java Mail API
 
Similar Threads
how to format a normal text file by java program
Create rtf document from a template
converting xml into rtf
How to update an RTF template file in java?
how to read rtf file and display in console?