Upload .eml files to javax.mail.Messages

I am trying to use the unit test method that processes instances javax.mail.Message.

I am writing a converter for modifying emails that arrive in different formats and then convert to a consistent internal format ( MyMessage). This conversion will usually depend on the address of the address or email address, and to create a new one, MyMessagepart of the email address, subject and sender and response address will be required.

I have a collection of raw emails that are saved locally as files .eml, and I would like to do a unit test that loads files .emlfrom the class path and converts them to instances javax.mail.Message. Is this possible, and if so, how to do it?

+5
source share
2 answers

My problem came from using Mockito for the bullying javax.mail.Folderrequired by the constructor javax.mail.internet.MimeMessage MimeMessage(Folder, InputStream, int). This calls the constructor for javax.mail.Message Message(Folder, int), which then accesses folder.store.session. This led to the designer MimeMessagechoosing NullPointerException.

Decision:

class ClasspathMimeMessage extends MimeMessage {
    private ClasspathMimeMessage(Folder folder, InputStream is, int msgnum) throws MessagingException {
        super(folder, is, 0);
    }

    public static MimeMessage create(String resourceName) {
        Class<PopEmailMmsReceiverTest> loaderClass = PopEmailMmsReceiverTest.class;
        InputStream is = loaderClass.getResourceAsStream(resourceName);

        Folder inbox = new MyFolder();

        try {
            return new ClasspathMimeMessage(inbox, is, 0);
        } catch (MessagingException ex) {
            throw new RuntimeException("Unable to load email from classpath at " + loaderClass.getResource(resourceName).toString());
        }
    }
}

class MyFolder extends Folder {
    MyFolder() {
        super(createMockStore());
    }
    private static Store createMockStore() {
        return mock(Store.class);
    }
    public void appendMessages(Message[] msgs) throws MessagingException {
    }
    public void close(boolean expunge) throws MessagingException {
    }
    public boolean create(int type) throws MessagingException {
        return false;
    }
    public boolean delete(boolean recurse) throws MessagingException {
        return false;
    }
    public boolean exists() throws MessagingException {
        return false;
    }
    public Message[] expunge() throws MessagingException {
        return null;
    }
    public Folder getFolder(String name) throws MessagingException {
        return null;
    }
    public String getFullName() {
        return null;
    }
    public Message getMessage(int msgnum) throws MessagingException {
        return null;
    }
    public int getMessageCount() throws MessagingException {
        return 0;
    }
    public String getName() {
        return null;
    }
    public Folder getParent() throws MessagingException {
        return null;
    }
    public Flags getPermanentFlags() {
        return null;
    }
    public char getSeparator() throws MessagingException {
        return 0;
    }
    public int getType() throws MessagingException {
        return 0;
    }
    public boolean hasNewMessages() throws MessagingException {
        return false;
    }
    public boolean isOpen() {
        return false;
    }
    public Folder[] list(String pattern) throws MessagingException {
        return null;
    }
    public void open(int mode) throws MessagingException {
    }
    public boolean renameTo(Folder f) throws MessagingException {
        return false;
    }   
}

It looks very ugly to me, so if anyone has a better suggestion, I would be happy to hear that.

0
source

After several tests, I finally successfully downloaded the message using the open constructor MimeMessage(Session, InputStream)(unlike the protected one based on the Folder specified in another answer).

import java.io.FileInputStream;
import java.io.InputStream;

import javax.mail.internet.MimeMessage;

public class LoadEML {

    public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream(args[0]);
        MimeMessage mime = new MimeMessage(null, is);
        System.out.println("Subject: " + mime.getSubject());
    }

}
+9
source

All Articles