【发布时间】:2016-08-27 21:43:46
【问题描述】:
我正在使用范围:mail.send、mail.readwrite、mail.read、offline_access、openid、email 和 profile(虽然我相当自信我不需要所有这些 -> 目标是阅读收件箱和发送电子邮件,同时获取电子邮件和姓名(如果存在)。
然后我使用以下代码连接到 SMTP 服务器:
OAuth2Authenticator.connectToSmtp("smtp-mail.outlook.com",
587,
user.getOutlookUid(),
accessToken,
true);
实际连接服务器的代码在这里:
public static SMTPTransport connectToSmtp(String host, int port, String userEmail, String oauthToken, boolean debug)
throws Exception {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put("mail.smtp.sasl.mechanisms.oauth2.oauthToken", oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
String password = "";
transport.connect(host, port, userEmail, password);
return transport;
}
好的,现在我可以进入最令人沮丧的部分了...我已经使用“connectToSMTP”方法连接到 Gmail,并且效果很好。
OAuth2Authenticator.connectToSmtp("smtp.gmail.com",
587,
user.getGoogleUid(),
accessToken,
true);
所以最终我的问题是“我做错了什么?”或“我可以更新什么才能通过 Outlook 发送电子邮件”?我已经看到 Outlook 有一个 REST API,但那是 B 计划。Outlook 与 Gmail 有什么不同吗?
我考虑过的一些事情:
- Scope 没有请求足够的访问权限(所以我现在可能要求太多了)
- access_token 存储不正确或以某种方式编码(尝试从 base_64 解码它,但什么也没提供)。我可以使用我的 refresh_token 来更新 access_token 以便告诉我我可能正确地存储了它们。
- 我尝试为密码传递 null。还传递了实际密码并且有效,但我有 access_token 和 refresh_token 所以我不需要询问他们的明确密码。此外,向用户询问这将是危险和粗略的。
- 我尝试使用“openssl s_client -crlf -starttls smtp -connect smtp-mail.outlook.com:587”手动连接到 smtp 服务器,但似乎认为我的 access_token 错误“535 5.0.0 OAuth failed:由于令牌无效,OAuth 身份验证失败。代码 -2147184118" 采用二进制补码并转换为十六进制时,该数字为 0x8004920a。帮助搜索但无济于事。
- 我为此做了很多搜索,现在将继续在各处发布。它与 Gmail 一起使用有很多资源,但如前所述,我已经将它用于 Gmail。 Outlook 似乎有些不同。此外,我遇到了很多关于电子邮件客户端上电子邮件转发的帖子......我正在半创建一个电子邮件客户端,所以通过 outlook.com 设置对我没有帮助。
我的一个朋友担心的另一个问题是我的访问令牌真的很长,这导致了手动 smtp 服务器声称的内容。它有 1188 个字符长。类似于 'EwB4Aul3BAAUo4xeBIbHjhBxWOFekj4Xy2...x9stHxi2K/VFggE=' (显然我隐藏了大部分字符)。
感谢任何提供建议或发现我的问题的人。特别是为什么我可以传递电子邮件密码并且失败,但使用 oauth access_token 失败。
【问题讨论】:
标签: java oauth outlook smtp email