gpt4 book ai didi

java - 通过java发送邮件遇到错误

转载 作者:行者123 更新时间:2023-12-01 11:34:07 24 4
gpt4 key购买 nike

现在我遇到了很奇怪的问题,那就是在发送邮件期间这很奇怪,因为当我在本地主机上测试我的应用程序时,一切都很完美,但是当我部署我的应用程序时,它能够发送邮件。

大家也可以看看 http://www.mkjit-solutions.com/app/jsf/Contactus.xhtml

我看到这个异常:

javax.mail.SendFailedException: Sending failed; nested exception is:
class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. yv6sm13782372pac.29 - gsmtp

我使用的代码是

props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.port", "587");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");


Session session = Session.getInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userAddress, userPassword);
}
});

customMsg += "3 - ";

try {

customMsg += "3.1 - ";
Message message = new MimeMessage(session);

customMsg += "3.2 - ";
message.setFrom(new InternetAddress(userAddress));

customMsg += "3.3 - ";
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress));

customMsg += "3.4 - ";
message.setSubject(subject);

customMsg += "3.5 - ";
message.setText(msg);

customMsg += "3.6 - ";
Transport transport = session.getTransport("smtp");
Transport.send(message);

customMsg += "4 - ";


System.out.println("ua"+userAddress);
System.out.println(""+userPassword);
System.out.println("ta"+toAddress);




System.out.println("Done");
flag = true;
customMsg += " [Value of Flag "+flag+"] ";
}
catch (Exception ex)
{
System.out.println("");
ex.printStackTrace();
flag = false;
customMsg += " [Value of Flag "+flag+"] ";
this.e = ex;
//throw new RuntimeException(e);

}

return flag;

最佳答案

首先修复这些 common mistakes .

Gmail 示例代码位于 JavaMail FAQ .

如果您仍然无法让它工作,请按照这些 debugging tips 操作,并发布结果。

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

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