gpt4 book ai didi

java - 通过 Javamail 发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 17:25:57 26 4
gpt4 key购买 nike

我一直在尝试使用 javamail api 发送电子邮件。 smtp 服务器 (smtp.live.com) 的调试显示 550 5.3.4 未执行请求的操作;要继续发送消息,请登录您的帐户。

它似乎可以很好地创建消息,但不允许其发送。有什么想法吗?

  try
{
// Setup properties for e-mail server
Properties props = System.getProperties();
props.put("mail.smtp.host", mConfig.getEmailHost());
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");

// Get a Session object
Session session = Session.getInstance(props, new MyAuthenticator());
session.setDebug(true);
Transport transport = session.getTransport("smtp");

// Create message
MimeMessage message = new MimeMessage(session);

// Add the to/from fields
message.setFrom(new InternetAddress(mFromAddr, mFromName));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mToAddr));
if (mCCAddrs != null)
{
for (int i=0; i<mCCAddrs.length; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(mCCAddrs[i]));
}
// Add Subject
message.setSubject(mEmailSubject);

// Setup multipart message for including the attachment
Multipart multipart = new MimeMultipart();

// Create message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(mEmailBody);
multipart.addBodyPart(messageBodyPart);

if (mAttachmentName != null)
{
// Create message attachment
BodyPart messageAttachmentPart = new MimeBodyPart();
messageAttachmentPart.setDataHandler(new DataHandler(new ByteArrayDatasource(data)));
messageAttachmentPart.setFileName(mAttachmentName);
multipart.addBodyPart(messageAttachmentPart);
}

// Send message
message.setContent(multipart);
transport.connect(mConfig.getEmailHost(), mConfig.getEmailUser(), mConfig.getEmailPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (Exception ex)
{
ex.printStackTrace();
throw new Exception("Failed to send e-mail: " + ex.getMessage());
}
`

最佳答案

您实际上需要在身份 validator 中包含一些凭据信息。服务器向您表明它不允许匿名发送电子邮件。

new MyAuthenticator()

^-------- fill this with some credentials

请注意,除非您的邮件服务器有一些特殊要求,否则通常使用standard password authenticator就足够了。 :

<小时/>

编辑/更新

根据您的反馈,我仔细查看了您的错误消息。在我看来,Hotmail 要求您先登录已设置的帐户并进行验证,然后再使用它发送电子邮件。在使用该帐户之前,您可能需要使用网络浏览器登录并检查其中是否有激活链接。

关于java - 通过 Javamail 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14979886/

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