gpt4 book ai didi

java - 使用 Java 使用 hotmail 帐户发送电子邮件

转载 作者:行者123 更新时间:2023-12-05 07:51:37 26 4
gpt4 key购买 nike

我正在做一个使用 hotmail 帐户发送电子邮件的网络应用程序。这是我的代码:

props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.auth", true);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.user", "mail@hotmail.com");
props.put("mail.smtp.pwd", pass);
props.put("mail.debug", true);
props.put("mail.smtp.starttls.enable", "true");

Session session = Session.getDefaultInstance(props);

session.setDebug(true);

try {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("othermail@otherserver.com"));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("mail@hotmail.com"));
mimeMessage.setSubject(subject);
mimeMessage.setContent(message, "text/html; charset=UTF-8");
mimeMessage.setSentDate(new Date());

SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect("smtp.live.com", "mail@hotmail.com", pass);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
} catch (MessagingException e) {
ret = false;}

当我在本地使用此代码时,一切正常,但当我在托管服务器上部署我的应用程序时,出现以下跟踪:

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.live.com", port 587, isSSL false
220 BLU436-SMTP170.smtp.hotmail.com Microsoft ESMTP MAIL Service, Version: 8.0.9200.16384 ready at Thu, 14 Jan 2016 00:04:21 -0800
DEBUG SMTP: connected to host "smtp.live.com", port: 587

EHLO sbi02.namesservers.net
250-BLU436-SMTP170.smtp.hotmail.com Hello [37.187.179.160]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-TLS
250-STARTTLS
250 OK
DEBUG SMTP: Found extension "TURN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8bitmime", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "TLS", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "OK", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO sbi02.namesservers.net
250-BLU436-SMTP170.smtp.hotmail.com Hello [37.187.179.160]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-AUTH LOGIN PLAIN XOAUTH2
250 OK
DEBUG SMTP: Found extension "TURN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8bitmime", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2"
DEBUG SMTP: Found extension "OK", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
535 5.0.0 Authentication Failed

我很确定帐户凭据是正确的,但身份验证失败。另外我不得不说,当我尝试这样做时,我收到一封微软的电子邮件,说有人试图启动 session ,但是当我在本地执行代码时,我没有收到这封电子邮件。我查看了页面帐户信息和安全和隐私部分,看到我最近的 Activity 出现了关于此尝试发送电子邮件的挑战安全性。在此信息中,设备和平台是未知的,IP 是我的主机。有什么办法可以做到这一点?

最佳答案

我只找到了解决我的问题的可能方法。我在帐户中启用了两步验证。启用此选项后,hotmail 会为您提供创建应用程序密码的选项。这个密码是我在我的应用程序中使用的密码,而不是 hotmail 帐户的真实密码。此解决方案也适用于 Gmail 帐户。

关于java - 使用 Java 使用 hotmail 帐户发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34785639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com