我是java邮件的新手,我一直在努力尝试从邮件服务器下载附件。我正在使用java邮件1.5.5我一直在互联网上搜索如何连接到邮件服务器并下载电子邮件和附件,我有这样的代码:
Properties props = new Properties();
props.put("mail.store.protocol", "pop3");
Session session = Session.getInstance(props);
session.setDebug(true);
store = session.getStore();
store.connect("my.mail.server", "myusername", "mypassword");
folder = store.getDefaultFolder().getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; ++i) {
System.out.println("MESSAGE #" + (i + 1) + ":");
Message msg = messages[i];
System.out.println(msg.getContent().getClass());
System.out.println(msg.getDisposition());
System.out.println(msg.getFileName());
System.out.println(msg.getContentType());
System.out.println(msg.getFrom());
System.out.println(msg.getReceivedDate());
System.out.println(msg.getReplyTo());
System.out.println(msg.getSentDate());
}
,此代码的结果是:
content: class java.lang.String
disposition: null
fileName: null
contentType: text/plain
from: null
receivedDate: null
replyTo: null
sendDate: null
我不确定,但是我不应该至少收到消息的“发件人”或“发送日期”?为什么这些字段为空?我做错了什么?
邮件内容属于String
。
请告诉我我做错了什么,谢谢。
修改
这是调试日志:
Loading javamail.default.providers from jar:file:/D:/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/my_web/WEB-INF/lib/javax.mail-1.5.5.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Oracle, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Oracle, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Oracle, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Oracle, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Oracle, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Oracle, version=null
Loading javamail.default.providers from jar:file:/D:/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/my_web/WEB-INF/lib/mail-1.4.7.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Oracle, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Oracle, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Oracle, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Oracle, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Oracle, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Oracle, version=null
DEBUG: getProvider() returning provider protocol=pop3; type=javax.mail.Provider$Type@7e56e2bd; class=com.sun.mail.pop3.POP3Store; vendor=Oracle
DEBUG POP3: mail.pop3.rsetbeforequit: false
DEBUG POP3: mail.pop3.disabletop: true
DEBUG POP3: mail.pop3.forgettopheaders: false
DEBUG POP3: mail.pop3.cachewriteto: false
DEBUG POP3: mail.pop3.filecache.enable: false
DEBUG POP3: mail.pop3.keepmessagecontent: false
DEBUG POP3: mail.pop3.starttls.enable: true
DEBUG POP3: mail.pop3.starttls.required: false
DEBUG POP3: mail.pop3.apop.enable: false
DEBUG POP3: mail.pop3.disablecapa: false
DEBUG POP3: connecting to host "mail.mymail.am", port 110, isSSL false
+OK Dovecot ready.
CAPA
+OK
CAPA
TOP
UIDL
RESP-CODES
PIPELINING
AUTH-RESP-CODE
STLS
USER
SASL PLAIN LOGIN
.
DEBUG POP3: PIPELINING enabled
STLS
+OK Begin TLS negotiation now.
CAPA
+OK
CAPA
TOP
UIDL
RESP-CODES
PIPELINING
AUTH-RESP-CODE
USER
SASL PLAIN LOGIN
.
DEBUG POP3: authentication command trace suppressed
DEBUG POP3: authentication command succeeded
STAT
+OK 73 5815632