使用OAuth和JavaMail发送电子邮件

时间:2014-06-05 17:43:25

标签: java smtp javamail

我有一个Outlook.com电子邮件帐户。我希望我的Java应用程序能够使用JavaMail从该电子邮件帐户发送电子邮件。为此,我通过OAuth授予了我的应用访问权限。

我已经能够使用msgshow.java中提供的精彩示例应用程序通过IMAP成功连接。但是,我尝试通过SMTP通过我的Outlook.com电子邮件帐户发送电子邮件,而且我没有运气。我尝试过使用msgsend.java和smtpsend.java应用程序。但是,我没有运气。我试过了:

java -Dmail.smtp.starttls.enable=true msgsend -d -M smtp-mail.outlook.com -U myAddress@outlook.com -P [oauthAccessToken] recipient@domain.com

返回:

550 5.7.3 Requested action aborted; user not authenticated
DEBUG SMTP: got response code 550, with response: 550 5.7.3 Requested action aborted; user not authenticated

RSET
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.3 Requested action aborted; user not authenticated
;
  nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 550 5.7.3 Requested action aborted; user not authenticated

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2202)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1693)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1194)
    at javax.mail.Transport.send0(Transport.java:254)
    at javax.mail.Transport.send(Transport.java:124)
    at msgsend.main(msgsend.java:188)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 550 5.7.3 Requested action aborted; user not authenticated

    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1700)
    ... 4 more

我也尝试过:

java -Dmail.smtp.starttls.enable=true smtpsend -d -A -M smtp-mail.outlook.com -U myAddress@outlook.com -P [oauthAccessToken] recipient@domain.com

我得到了:

DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
Got Exception: javax.mail.AuthenticationFailedException: 535 5.0.0 Authentication Failed

如何使用OAuth通过Outlook.com通过JavaMail发送电子邮件?

谢谢!

1 个答案:

答案 0 :(得分:0)

你看到了instructions on the JavaMail wiki吗?它们展示了如何在Java中执行此操作,但您可以使用msgshow.java通过在命令行上设置属性来执行相同操作,例如“-Dmail.smtp.auth.login.disable = true”。