Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sending attachments with emails in Javamail

843834Jan 27 2009 — edited Jan 28 2009
I'm trying to send attachments with emails using Javamail. Following is the code through which I'm trying to achieve that. It works as expected on a JRE1.6 environment. But on JRE1.5, the content of the file gets added to the mail body as text.I want the file to be sent as an attachment.

Any pointers on the observed difference in behavior would be highly appreciated!
 String msgText = mailInfo.getMessage();
        String attachmentFileName = mailInfo.getFileName();
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setText(msgText);
        // create the second message part
        MimeBodyPart attachmentBodyPart = new MimeBodyPart();
        // attach the file to the message

        FileDataSource fileDataSource = new FileDataSource(attachmentFileName);
        attachmentBodyPart.setFileName(fileDataSource.getName());
        attachmentBodyPart.setDataHandler(new DataHandler(fileDataSource));
        

        // create the Multipart and add its parts to it
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mimeBodyPart);
        multipart.addBodyPart(attachmentBodyPart);

        message.setContent(multipart);
The email in case of JRE1.5 is as follows

{color:#0000ff}------=_Part_0_33189144.1233078680250
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi...Test Mail
------=_Part_0_33189144.1233078680250
Content-Type: application/octet-stream; name=corba_architecture.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=corba_architecture.pdf
Content-ID: Attachment

JVBERi0xLjIgDSXi48/TDQogDTggMCBvYmoNPDwNL0xlbmd0aCA5IDAgUg0vRmlsdGVyIC9GbGF0
ZURlY29kZSANPj4Nc3RyZWFtDQpIiUWPW07DMBBFV+A93E9QlWBP/Kj5awt8USFRbyBKnTQIkshK
YfvYcQoaybrSzDkzFhCxQgemeWkUyKTXEIeSHMGjZXvHSFBJCpXUpQLcE+NIlbCHFw4pUxeuZQUv
ueY25gYxk9mKmH9wtwvNpZ99M1+jc2wxXzxweHvf73AP9xGFhaIsTyC3/w6ZDYfxaxoHP8w4jmf/

......

------=_Part_0_33189144.1233078680250-- {color}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 25 2009
Added on Jan 27 2009
14 comments
492 views