ImapMailReceiver 的不当行为

Inappropriate behaviour of ImapMailReceiver

如果密码包含@,ImapMailReceiver 无法return 正确托管。

例如:

username: abc@gmail.com

password: abc@123

host: imap.gmail.com

最终 URI 字符串

imap://abc@gmail.com:abc@123@imap.gmail.com:993/INBOX

然后,ImapMailReceiver 将主机识别为 123@imap.gmail.com

我检查了 this 线程,但我使用 JavaConfig 方式创建 ImapMailReceiver,因为线程大约是 XML 配置方式。

有什么方法可以解决这个问题?

谢谢!

更新

带编码的最终 URI 字符串

imap://abc%40gmail.com:abc%40123@imap.gmail.com:993/INBOX

在这种情况下,我得到了 AuthenticationException。

以上信息可能有助于您理解问题。

更新 1

以上是 Gmail 的问题。需要在 Gmail 中启用 LESS SECURE APP 安全选项以修复身份验证相关问题。

RFC 1738 说:

Within the user and password field, any ":", "@", or "/" must be encoded.

我希望您已经知道 URL 编码的工作原理。如果您想复习一下,同一文档的第 18 页是一个不错的起点。

我在 Office 365 帐户中遇到了同样的问题,其中用户名类似于 'your-user@your-company.com',主机为 'outlook.office365.com'。没有对用户名进行编码,'your-company.com' 被用作主机,这会导致以下(有点误导)错误:

javax.mail.AuthenticationFailedException: failed to connect, no password specified?

激活调试 mail.debug=true ...

mailProps.put("mail.debug", "true");
receiver.setJavaMailProperties(mailProps);

... 为我们提供了用于连接到邮件服务器的参数。

DEBUG IMAPS: protocolConnect returning false, host = your-company.com, user = your-user, password = <null>

为了解决这个问题,我使用 URLEncoder.encode 作为用户名和密码,例如:

...
new StringBuilder("imaps://")
   .append(URLEncoder.encode(mailSettings.getUser(), StandardCharsets.UTF_8.toString()))
   .append(":")
   .append(URLEncoder.encode(mailSettings.getPassword(), StandardCharsets.UTF_8.toString()))
...