• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAVAMAIL

 
saheli sen gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HOW DO I SEND A ATTACHMENT & MESSAGE BOTH TOGETHER USIN JAVAMAIL
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see the examples given along with the java mail application
------------------
I.K.VISHWANATH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic