2014-07-11 89 views
0

我想通过IMAP从Hotmail接收电子邮件,但我必须通过OAuth进行身份验证。从Microsoft文档,它支持,http://msdn.microsoft.com/en-us/library/dn440163.aspx;如何通过IMAP和OAuth从hotmail接收电子邮件?

但我不得不知道如何在java中使用它(通过Javamail)。我试图使用谷歌代码示例(正确的电子邮件和access_toket),但失败了。

String email = "[email protected]"; 
String oauthToken = "XXXXXXXX"; 

initialize(); 

IMAPStore imapStore = connectToImap("imap-mail.outlook.com", 
            993, 
            email, 
            oauthToken, 
            true); 
System.out.println("Successfully authenticated to IMAP.\n"); 


Folder folder = imapStore.getDefaultFolder(); 
folder.open(Folder.READ_ONLY); //****The linke throw exception**** 
for (Message msg : folder.getMessages()) { 
    String subject = msg.getSubject(); 
    System.out.println(":" + subject); 
} 

印刷系统控制台上的日志,

DEBUG: setDebug: JavaMail version 1.4 
DEBUG: mail.imap.fetchsize: 16384 
DEBUG: enable SASL 
DEBUG: SASL mechanisms allowed: XOAUTH2 
* OK Outlook.com IMAP4rev1 server version 17.4.0.0 ready (DUB451-IMAP183) 
A0 CAPABILITY 
* CAPABILITY IMAP4rev1 CHILDREN ID NAMESPACE UIDPLUS UNSELECT AUTH=PLAIN AUTH=XOAUTH2 SASL-IR 
A0 OK CAPABILITY completed 
IMAP DEBUG: AUTH: PLAIN 
IMAP DEBUG: AUTH: XOAUTH2 
DEBUG: protocolConnect login, host=imap-mail.outlook.com, [email protected], password=<non-null> 
IMAP SASL DEBUG: Mechanisms: XOAUTH2 
IMAP SASL DEBUG: SASL client XOAUTH2 
A1 AUTHENTICATE XOAUTH2 
+ 
IMAP SASL DEBUG: challenge: : 
IMAP SASL DEBUG: callback length: 1 
IMAP SASL DEBUG: callback 0: [email protected] 
IMAP SASL DEBUG: response: [email protected]=Bearer 

DEBUG: connection available -- size: 1 
A2 EXAMINE "" 
A1 NO [AUTHENTICATIONFAILED] OAuth authentication failed. 
A2 BAD Examine Command is not permitted in current state (NotAuthenticated) 
A3 LOGOUT 
* BYE Logout requested 
Exception in thread "main" javax.mail.MessagingException: A2 BAD Examine Command is not permitted in current state (NotAuthenticated); 
    nested exception is: 
    com.sun.mail.iap.BadCommandException: A2 BAD Examine Command is not permitted in current state (NotAuthenticated) 
    at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:829) 
    at com.google.code.samples.oauth2.OAuth2Authenticator.main(OAuth2Authenticator.java:155) 
Caused by: com.sun.mail.iap.BadCommandException: A2 BAD Examine Command is not permitted in current state (NotAuthenticated) 
    at com.sun.mail.iap.Protocol.handleResult(Protocol.java:296) 
    at com.sun.mail.imap.protocol.IMAPProtocol.examine(IMAPProtocol.java:636) 
    at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:811) 
    ... 1 more 

回答

0

我试验成功!我验证了3件事,

  1. 我们可以使用谷歌代码示例收集到Outlook的IMAP。示例代码的链接是https://code.google.com/p/google-mail-oauth2-tools/wiki/JavaSampleCode
  2. 必须使用范围wl.imap和wl.offline_access
  3. 在我的Hotmail acccount不工作,我甚至无法获得的access_token;但在我使用我的另一个真实帐户后,它可以正常工作。
+0

你确定你的答案在“1”的Hotmail工作? –

+0

@matant是的,我相信它仍然在现场。没有尝试hotmail帐户。 – Stony

+0

我如何使用范围wl.imap和wl.offline_access?它应该是我的Java代码的一部分? –

0

您正在使用旧版本的JavaMail。如果您使用latest版本,OAuth2 support is built in

+0

我justed试图javamail的1.5.2, \t javax.mail \t javax.mail-API \t 1.5.2;但不起作用。 Stony

+0

正如它在[JavaMail项目页面](https://java.net/projects/javamail/pages/Home)中所述,javax.mail-api是“仅适用于编译的JavaMail API定义”。您需要com.sun.mail:javax.mail,它是“JavaMail参考实现jar文件,包括SMTP,IMAP和POP3协议提供程序”。 –

相关问题