Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with java mail attachment

 
peter qiu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic