jakarta-mailemlsimple-java-mail

Why SimpleJavaMail cannot read eml file but JavaMail can


There is an answer that gives an example to read an eml file. I also found another example that uses plain JavaMail.

Below is my code that attempts both.

package mymailRead;

import jakarta.mail.Session;
import jakarta.mail.internet.MimeMessage;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import org.simplejavamail.api.email.Email;
import org.simplejavamail.converter.EmailConverter;

public class Main {

  public static void main(String[] args) {
    String emlFile = "C:/xyz/a.eml";
    System.out.println("Simple per simplejavamail.org ");
    readWithSimpleMail(emlFile);

    System.out.println("\n Java mail read ");
    readWithRegularMail(emlFile);
  }

  private static void readWithSimpleMail(String emlFile) {
    Email email = EmailConverter.emlToEmail(emlFile);
    System.out.println("From " + email.getFromRecipient());
    System.out.println("Subject " + email.getSubject());
    email.getPlainText();
    System.out.println(" email is " + email);
  }

  private static void readWithRegularMail(String emlFile) {
    try {
      Properties props = System.getProperties();
      props.put("mail.host", "smtp.dummydomain.com");
      props.put("mail.transport.protocol", "smtp");

      Session mailSession = Session.getDefaultInstance(props, null);
      InputStream source = new FileInputStream(emlFile);
      MimeMessage message = new MimeMessage(mailSession, source);

      System.out.println("From : " + message.getFrom()[0]);
      System.out.println("Subject : " + message.getSubject());
      System.out.println("--------------");
      System.out.println("Body : " + message.getContent());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

Below is the build.gradle

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'application'

mainClassName = 'mymailRead.Main'

repositories {
    mavenCentral()
}

dependencies {
  testImplementation     'junit:junit:4.13'
 
  implementation 'org.simplejavamail:simple-java-mail:8.0.0'
  implementation 'jakarta.mail:jakarta.mail-api:2.1.1'
  implementation 'org.eclipse.angus:jakarta.mail:2.0.1'
  implementation 'jakarta.activation:jakarta.activation-api:2.1.1'
  implementation 'org.slf4j:slf4j-api:2.0.6'
  implementation 'org.slf4j:slf4j-simple:2.0.6'
}

Below is the a.eml

X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Received: from tomts25-srv.bellnexxia.net
        (tomts25.bellnexxia.net [209.226.175.188])
    by tactika.com (8.9.3/8.9.3) with ESMTP id NAA07621
    for <real@rgagnon.com>; Sun, 1 Feb 2004 13:25:33 -0500 (EST)
Date: Sun, 01 Feb 2004 13:31:40 -0500
From: real gagnon <real@rgagnon.com>
Reply-To: real@rgagnon.com
User-Agent: Mozilla/5.0
   (Windows; U; Windows NT 5.1; en-US; rv:1.4)
   Gecko/20030624 Netscape/7.1 (ax)
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: real@rgagnon.com
Subject: Example for HowTo
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
X-UIDL: oP#!!c]^!!1;-!!T@1"!


This is an example for HowTo

Method readWithSimpleMail is returning an Email object that is not populated for any values. The program output is shown below.

Simple per simplejavamail.org 
From null
Subject 
 email is Email{
    id=null
    sentDate=null
    fromRecipient=null,
    replyToRecipient=null,
    bounceToRecipient=null,
    text='null',
    textHTML='null',
    textCalendar='null (method: null)',
    contentTransferEncoding='quoted-printable',
    subject='',
    recipients=[],
    headers={C=[/xyz/a.eml]}
}

 Java mail read 
From : real gagnon <real@rgagnon.com>
Subject : Example for HowTo
--------------
Body : 
This is an example for HowTo

Solution

  • This is because you used the .emlToMimeMessage(String) method which takes a string and assumed it should be the path to the EML file. However, if you follow the JavaDoc trail, you can read the String should be the EML data itself.

    If you want to load from file, call the overloaded version that takes a File. So to fix your case:

    File emlFile = new File("C:/xyz/a.eml");
    Email email = EmailConverter.emlToEmail(emlFile);
    

    I think in the future the String-based method should be able to determine whether it refers to a file or contains the data itself, but right now you can use the File-based method.