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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JAVAMAIL" Watch "JAVAMAIL" New topic
Author

JAVAMAIL

saheli sen gupta
Greenhorn

Joined: Nov 03, 2000
Posts: 13
HOW DO I SEND A ATTACHMENT & MESSAGE BOTH TOGETHER USIN JAVAMAIL
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by saheli sen gupta:
HOW DO I SEND A ATTACHMENT & MESSAGE BOTH TOGETHER USIN JAVAMAIL

hi saheli
using multipart class of javamail api you can send both message and attachment together.go through the following code it will give u some idea about message and attachment.
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(mailFrom));
InternetAddress[] address = {new InternetAddress(mailTo)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(message);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// 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);
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);
saheli sen gupta
Greenhorn

Joined: Nov 03, 2000
Posts: 13

Thanks , I have already tried out in the stated approach.Problem
was I coudn't get the the file as a attachment( content is diplayed properly) along with that the boundary is also diaplayed.
KASI VISHWANATH
Ranch Hand

Joined: Aug 06, 2000
Posts: 60
see the examples given along with the java mail application
------------------
I.K.VISHWANATH


I.K.VISHWANATH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAVAMAIL
 
Similar Threads
Receiving mail without using JavaMail
How to make an E-mail service?
Java with Lotus notes
problem in sending mail using jsp
I'm trying to make my code send me email - pliz help.