views:

2537

answers:

6

Hi.

I'll create a EML file with an attachment using JavaMail.

I created a simple EML file successfully, but adding an attachment don't work properly. I'm going to add a PDF file. My EML file will be created successfully. If I open the generated EML file with Outlook I'll find not my PDF file as attachment but I'll find the EML file itself as attachment. Does anyone has an idea?

I tried two variants (with same result), I used the FileDataSource class and the simple way with MimeBodyPart#attachFile(File).

I'm going to post an example:

File pdfFile = new File("somePdfFile");

Properties p = System.getProperties();
Session session = Session.getInstance(p);
MimeMessage message = new MimeMessage(session);
// MimeBodyPart txt = new MimeBodyPart();
// txt.setText("");
MimeBodyPart mbp = new MimeBodyPart();
mbp.attachFile(attachment);
// FileDataSource fds = new FileDataSource(attachment);
// fds.setFileTypeMap(new FileTypeMap() {
//    
//   @Override
//   public String getContentType(String arg0) {
//     return "application/pdf";
//   }
//    
//    @Override
//    public String getContentType(File file) {
//      return "application/pdf";
//    }
//      
//  });
//  mbp.setDataHandler(new DataHandler(fds));
//  mbp.setFileName("\"" + attachment.getName() + "\"");
//  mbp.setDisposition(MimePart.ATTACHMENT);
//  mbp.setHeader("Content-ID", "Attachment");
Multipart mp = new MimeMultipart();
//  mp.addBodyPart(txt);
mp.addBodyPart(mbp);
message.setContent(mp);
File emlFile = new File("message.eml");
emlFile.createNewFile();
message.writeTo(new FileOutputStream(emlFile));

// do something with the EML file
// Desktop.getDesktop().open(emlFile);

Thank you in advance for any help and ideas.
Best Regards
Zubi

(http://stackoverflow.com/questions/157195/create-an-eml-e-mail-file-in-java)

+1  A: 

Can you post the email file that got generated? Being that it's probably TNEF encoded, it will be a binary file, but I'm courious what output your code generated.

When you call mbp.attachFile(), make sure that the function is setting the mime type correctly, etc. If not, you will have to set it for that mime part, and you probably want it to be "attachment", (which may not be implied, even though you called attachFile(), as that function can also be used to attach HTML inline, etc).

(I think it's the content-disposition that you want to set to "attachment", and the content-type to be the files mime-type, "application/pdf". You may also want, or need to set the filename= property).

Larry

LarryF
A: 

Hi Larry

Thank you for your response. I uploaded a PDF file (that I use for testing, it's a simple HelloWorld generated with Crystal Reports) and the generated EML file which should include the PDF file.

I just noticed that if I open the linked EML file with Apple Mail or with Outlook Express it works (but without edit possibility). Maybe it's an issue of Microsoft Outlook?

The links are removed

Zubi
+1  A: 

Zubi, it looks like the issue is the content type on the attachment is set to "application/octet-stream". So, it looks like the mail reader is taking the PDF file as an alternate display for the "text" body of the message which does not exist, (it's just blank).

You'll have to forgive me, it's been more than a year since I've dealt with Mime, but I think you are going to want to A) Put some body text in the message, B) Make sure the type on the attachment is set to application/pdf. Hopefully, this will prevent the mail reading from trying to display the PDF as the primary body of the message.

Other than that, it looks normal... Now, Outlook MIGHT bitch because there are no RFC 822 headers in the main body. You may want to add at LEAST a From:, To:, and a Subject:.

The message passed MY mime parsing code...

Larry

LarryF
A: 

Hi Larry

Thank you.
I tried it one more time with the FileDataSource but without any success.

Message-ID: <23146591.1.1231404007293.JavaMail.USER@USER>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_Part_0_22060939.1231404007262"

------=_Part_0_22060939.1231404007262
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

E-Mail created by Application
------=_Part_0_22060939.1231404007262
Content-Type: application/pdf; name=HelloWorld_08012009094007.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=HelloWorld_08012009094007.pdf
Content-ID: Attachment

JVBERi0xLjMgCiXi48/TIAo3IDAgb2JqCjw8Ci9Db250ZW50cyBbIDggMCBSIF0gCi9QYXJlbnQg
....

Everything looks fine in the EML file but it won't work. For this reason I decided to skip this implementation and startover with a own small mail sender. Thank you anyway for your help!!!

Thomas

Zubi
+1  A: 

You should try adding the header lines I mentioned to the very top of the message and see how Outlook deals with it then. Add a To:, From:, Subject: and maybe even a Date: with real data in them, and Outlook is more likely to treat it as a message, rather that just a file.

LarryF
A: 

Thank you Larry. I didn't tried your last post. Finally we decided to skip this possibility to send by default mail client. We are going to use JavaMail in our project to send the PDF file.

Zubi