Javamail和Gmail获取消息
我有一个大学的gmail帐号 该账户由Javamail和Gmail获取消息,java,jakarta-mail,Java,Jakarta Mail,我有一个大学的gmail帐号 该账户由student.university.com 所以如果我试着剪下这个: Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); try { Session session = Session.getDefaultInstance(props, null);
student.university.com
所以如果我试着剪下这个:
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("mail.gmail.com", "username", "password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());
} catch (Exception mex) {
mex.printStackTrace();
}
但如果我试图改变这一点,它就不起作用了:
store.connect("student.university.com", "username", "pass");
还是不行。。。我错在哪里
编辑:2分钟后,我出现以下错误:
javax.mail.MessagingException: Connection timed out;
nested exception is:
java.net.ConnectException: Connection timed out;
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at RetriveMail.main(RetriveMail.java:17)
如果我尝试使用Debug,我有:
DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 151.40.212.208 p48if11656880wer.170
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
A0 OK Thats all she wrote! p48if11656880wer.170
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: PLAIN-CLIENTTOKEN
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user=username@student.university.com, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)
DEBUG IMAPS:尝试连接到主机“imap.gmail.com”,端口993,isSSL true
*OK Gimap准备好接受来自151.40.212.208 P48IF1165680WER.170的请求
A0能力
*功能IMAP4rev1取消选择空闲命名空间配额ID XLIST子项X-GM-EXT-1 XYZZY SASL-IR AUTH=XOATH AUTH=XOATUH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
好的,她就写这些!P48IF1165680WER.170
调试IMAPS:AUTH:XOAUTH
调试IMAPS:AUTH:XOAUTH2
调试IMAPS:AUTH:PLAIN
调试IMAPS:AUTH:PLAIN-CLIENTTOKEN
调试IMAPS:protocolConnect登录,主机=imap.gmail.com,用户=username@student.university.com,密码=
调试IMAPS:取消验证普通命令跟踪
调试IMAPS:验证普通命令结果:A1无[AUTHENTICATIONFAILED]无效凭据(失败)
它不工作怎么办?各位,真的很难想象事情会以多种方式失败吗?我没有任何反应。如果我尝试使用另一个帐户,但由Google direct works管理。@FelasDroid:定义“它不起作用”。错误是什么?当你调试这个时,它以什么方式失败?我编辑了带有错误的帖子