使用javamail读取gmail收件箱,可以在Eclipse中使用,但不能在外部使用 [英] read gmail inbox using javamail, works in eclipse but not outside

查看:84
本文介绍了使用javamail读取gmail收件箱,可以在Eclipse中使用,但不能在外部使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码连接到一个Gmail帐户,并读取收到的每封电子邮件.我检索电子邮件的内容.我为此使用javamail api.

My code connects to a gmail account and reads each email received. I retrieve the content of the email message. I use javamail api for that.

当我从eclipse运行代码时,它工作得很好.但是,如果我将所有内容导出到jar文件中,然后从命令提示符运行,则会收到以下错误:

When I run the code from eclipse, it works absolutely fine. But if I export everything into a jar file and then run from command prompt, then I get the following error :

java.lang.ClassCastException: com.sun.mail.imap.IMAPInputStream cannot be cast to javax.mail.Multipart

在以下行中:

Multipart mp = (Multipart) msg.getContent();

我尝试使用此方法,但无济于事:

I tried using this but it doesn't help :

public static void main(String[] args) throws Exception {

    System.out.println("Begin");        

    Thread.currentThread().setContextClassLoader(getOldIds.class.getClassLoader());
    //read emails
    }


Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");

        Session session = Session.getInstance(props, null);

        Store store = session.getStore("imaps");
        store.connect("imap.gmail.com", -1, "username", "password");
                    //fetch the message from the inbox

请建议我应该怎么做.

谢谢

推荐答案

您如何将所有内容导出到jar文件中?

How are you exporting everything into a jar file?

如果要将mail.jar文件中的所有应用程序类和所有JavaMail类组合到一个新的jar文件中,则会丢失mail.jar文件中用于配置MIME类型映射的资源文件到Java类.

If you're combining all your application classes and all the JavaMail classes from the mail.jar file into a single new jar file, you're missing the resource files from the mail.jar file that configure the mapping from MIME type to Java class.

您的应用程序类应位于一个jar文件中,并且该jar文件应在应用程序运行时(例如,在CLASSPATH上)引用或使用JavaMail jar文件.

Your application classes should be in one jar file and that jar file should reference or use the JavaMail jar file, e.g., on the CLASSPATH when the application runs.

这篇关于使用javamail读取gmail收件箱,可以在Eclipse中使用,但不能在外部使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆