The moose likes Java in General and the fly likes problem with java mail attachment Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "problem with java mail attachment" Watch "problem with java mail attachment" New topic
Author

problem with java mail attachment

peter qiu
Greenhorn

Joined: Dec 10, 2002
Posts: 4
i used java mail api to program email sending. Now i have the problem that the attachment could not be treated as the independent attached file but displayed following the main text as the meanless characters, no matter what type of attachment i include, zip, au files. The main codes are:
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.addHeader("CONTENT-TYPE","multipart/mixed");
Transport tr = session.getTransport("smtp");
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.addHeader("CONTENT-TYPE","text/plain");
mbp1.setText("hello world");
FileDataSource fds = new FileDataSource(somefilename);
MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.addHeader("CONTENT-TYPE","application/octet-stream");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
int index = 0;
mp.addBodyPart(mbp1,index++);
mp.addBodyPart(mbp2,index++);
msg.setContent(mp);
tr.sendMessage(msg,msg.getAllRecipients());
tr.close();
Thanks!
peter qiu
Greenhorn

Joined: Dec 10, 2002
Posts: 4
if i use "Transport.send(msg);" instead of "tr.sendMessage(msg,msg.getAllRecipients());", the attachment file shows ok, but the authentication process which must use a Transport instance can't be done properly. How can i resolve this conflict?
Detlev Beutner
Ranch Hand

Joined: Jul 13, 2001
Posts: 76
I do not really know where the problem could be, but have a look at my approach:

There is no need to set the CONTENT-TYPE manually.
Hope it helps
Detlev
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with java mail attachment
 
Similar Threads
File attachments using Java Mail API
Sending mail through Exchange Server
Attach a file using java api
JAVAMAIL
Problem attaching a File