title: Creation of a EML file with an attachment using JavaMail
body: <p>I'll create a EML file with an attachment using JavaMail. </p>
  
  <p>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? </p>
  
  <p>I tried two variants (with same result), I used the <code>FileDataSource</code> class and the simple way with <code>MimeBodyPart#attachFile(File)</code>. </p>
  
  <p>I'm going to post an example:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/157195/create-an-eml-e-mail-file-in-java">Create a .eml (email) file in Java</a></p>
  
  <hr>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p><em>The links are removed</em></p>