将 .eml 文件加载到 javax.mail.Messages 中

发布于 2024-08-31 09:12:14 字数 308 浏览 5 评论 0原文

我正在尝试对处理 javax.mail.Message 实例的方法进行单元测试。

我正在编写一个转换器来更改以不同格式到达的电子邮件,然后将其转换为一致的内部格式(MyMessage)。此转换通常取决于电子邮件的发件人地址或回复地址,并且创建新的 MyMessage

我有一组原始电子邮件,它们在本地保存为 .eml 文件,我想进行一个单元测试,从类路径加载 .eml 文件并将它们转换为 javax.mail.Message 实例。这可能吗?如果可以,该怎么做?

I'm trying to unit test a method which processes javax.mail.Message instances.

I am writing a converter to change emails which arrive in different formats and are then converted into a consistent internal format (MyMessage). This conversion will usually depend on the from-address or reply-address of the email, and the parts of the email, the subject, and the from- and reply-addresses will be required for creating the new MyMessage.

I have a collection of raw emails which are saved locally as .eml files, and I'd like to make a unit test which loads the .eml files from the classpath and converts them to javax.mail.Message instances. Is this possible, and if so, how would it be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第几種人 2024-09-07 09:12:14

经过几次测试后,我终于使用 MimeMessage(Session, InputStream) 公共构造函数(而不是其他响应中引用的基于文件夹的受保护构造函数)成功加载了一条消息。

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());
    }

}

After a few tests, I finally successfully loaded a message using the MimeMessage(Session, InputStream) public constructor (as opposed to the Folder-based protected one cited in the other response).

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());
    }

}
冬天旳寂寞 2024-09-07 09:12:14

我的问题来自于使用 Mockito 来模拟 javax.mail.internet.MimeMessage 的构造函数 MimeMessage(Folder, InputStream, int ) 所需的 javax.mail.Folder。这将调用 javax.mail.Message Message(Folder, int) 的构造函数,然后访问 folder.store.session。这导致 MimeMessage 的构造函数抛出 NullPointerException

解决方案:

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;
    }   
}

这对我来说看起来很难看,所以如果有人有更好的建议,我会很高兴听到。

My problem came from using Mockito to mock the javax.mail.Folder required by javax.mail.internet.MimeMessage's constructor MimeMessage(Folder, InputStream, int). This calls the constructor for javax.mail.Message Message(Folder, int) which then accesses folder.store.session. This resulted in a NullPointerException being thrown by the constructor for MimeMessage.

Solution:

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;
    }   
}

This looks very ugly to me, so if anyone has a better suggestion, I'd be delighted to hear it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文