bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)

Home Page:http://www.simplejavamail.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error on missing mimetype for attachments when parsing Outlook messages where mimeTag was not included

deeev opened this issue · comments

commented

Hi guys,
I'm getting a "org.simplejavamail.converter.internal.msgparser.OutlookMessageException: Unable to parse Outlook message" exception when parsing a file, which is a msg file I save from outlook, using EmailConverter().outlookMsgToMimeMessage().

With another filepath (which seems wrong) I get a mimetype is required exception.

Could anyone help me troubleshooting?

Best regards
Deeev

commented

i looked a bit into it, the exception comes after the EmailPopulatingBuilder parses attachments, and a valid attachment has the mimTag=null

Line 318 of the EmailConverter builder.withAttachment

I'm unsure what to do since you closed the issue. Was it a user problem or a problem with the library?

commented

Oops, I mixed up the issue with one of my own projects, sorry!!

I think the problem is with the library. When I parse an msg file without any attachments the converting itself throws no exception. Convertable msg files still show some issues, for example not parsing the sender correctly (field is null, although there is a sender).

Hi @deeev, could you please try to reproduce this problem with https://github.com/bbottema/outlook-message-parser 1.1.17? That's the library being used under water (nearly the same API).

If you can provide a sample .msg and the code you use to convert it, I'll dive into the problem and get it sorted out. Thanks so much!

commented

Sure, I'll dive right into it later today!

Hi @deeev, had any luck with this yet? An example with Simple Java Mail would also be fine as long as I can reproduce it!

Hi @bbottema

I was able to reproduce the issue from @deeev, with outlook-message-parser and with simplejavamail, only throws in simplejavamail due to precondition for mime type.

Sample code below, msg as attachment (sample-email.zip)

The msg was created in Outlook by creating a new email with subject, recipient, body and a .txt file as attachment, then saving it as msg.

public static void outlookMessageParser() throws Exception { // version 1.1.17, poi-scratchpad 4.0.1
  String inputFile = "c:/temp/lorem.msg";
  try (FileInputStream fis = new FileInputStream(inputFile)) {
    OutlookMessageParser parser = new OutlookMessageParser();
    OutlookMessage outlookMessage = parser.parseMsg(fis);
    for (OutlookAttachment outlookAttachment : outlookMessage.getOutlookAttachments()) {
      OutlookFileAttachment fileAttachment = (OutlookFileAttachment) outlookAttachment;
      System.out.println("Filename: " + fileAttachment.getFilename()); // output "Filename: ipsum.txt"
      System.out.println("Mime tag: " + fileAttachment.getMimeTag()); // output "Mime tag: null"
    }
  }
}

public static void emailConverter() throws Exception { // version 5.1.1, poi-scratchpad 4.0.1
  String inputFile = "c:/temp/lorem.msg";
  try (FileInputStream fis = new FileInputStream(inputFile);
      FileOutputStream fos = new FileOutputStream("c:/temp/lorem.eml")) {
    MimeMessage message = org.simplejavamail.converter.EmailConverter.outlookMsgToMimeMessage(fis);
    // Throws
    // Exception in thread "main" java.lang.IllegalArgumentException: mimetype is required
    // at org.simplejavamail.internal.util.Preconditions.checkNonEmptyArgument(Preconditions.java:15)
    // at org.simplejavamail.email.EmailPopulatingBuilder.withAttachment(EmailPopulatingBuilder.java:1212)
    // at org.simplejavamail.converter.EmailConverter.buildEmailFromOutlookMessage(EmailConverter.java:318)
    // at org.simplejavamail.converter.EmailConverter.outlookMsgToEmailBuilder(EmailConverter.java:101)
    // at org.simplejavamail.converter.EmailConverter.outlookMsgToEmail(EmailConverter.java:92)
    // at org.simplejavamail.converter.EmailConverter.outlookMsgToMimeMessage(EmailConverter.java:142)
    // at ...
    message.writeTo(fos);
  }

Kind regards and thanks
Stephan

That was very helpful, @stephan-merkli! So what you have is an Outlook email with an attachment that is missing a mimetype. I don't think that is technically valid!

Can you try with your version of Outlook if this happens for files other than text files as well? If an image or zip for example are stored correctly with mimetype, try the following: take an image or a zip or something, and give it the extension .txt. Is it stored with or without mimetype?

I'm not sure how to deal with this; I suppose when opening the email, Outlook tries to guess the mimetype by the file's extension?

/edit: seems like it: https://stackoverflow.com/a/44907420/441662

Some methods to determine mimetype based on filetype using the JDK:

Implemented enhancement in bbottema/outlook-message-parser#6 release 1.1.18. Updated Simple Java Mail accordingly in 5.1.4.

@deeev, @stephan-merkli, can you please verify the issue is solved?

Tested with outlook-message-parser 1.1.18, Output is Filename: ipsum.txt; Mime tag: text/plain.
And tested with simple-java-mail 5.1.4 and it doesn't fail anymore while converting the msg to eml.
Thank you for the fix.