【发布时间】:2018-03-26 10:54:47
【问题描述】:
我正在构建一个应用程序,以使用 JavaMail 在 Java 上使用 Yandex 发送和接收电子邮件。当我发送电子邮件时,我发现了一个问题,我不知道如何解决它。它只是弹出并错误提示535 5.7.8 Error: authentication failed: Invalid user or password!。
我的代码如下:
public static void sendFromYandex(String from, String pass, String to, String subject, String body) throws AddressException, MessagingException {
Properties props = System.getProperties();
String host = "smtp.yandex.com";
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.quitwait", "false");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setHeader("Content-Type", "text/plain; charset=UTF-8");
message.setSubject(subject, "UTF-8");
message.setText(body, "UTF-8");
try {
message.setFrom(new InternetAddress(from));
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException ae) {
} catch (MessagingException me) {
}
}
from 和 pass 是登录 Yandex 服务的凭据。
【问题讨论】:
-
去掉套接字工厂属性you don't need it。您也不需要 setHeader 调用,因为 setText 调用将为您执行此操作。您使用的是什么版本的 JavaMail?您的服务器认为您使用了错误的用户名或密码。显然你不同意。为什么?
-
@BillShannon 它说我发送的邮件是垃圾邮件,它会被自动拒绝。不知道怎么解决
-
“身份验证失败”并不意味着邮件是垃圾邮件。如果它在看到消息之前就失败了,它如何判断消息是否是垃圾邮件?如果您不再收到“身份验证失败”,并且由于其他原因而失败,即因为它是垃圾邮件而拒绝该邮件,那么请停止发送垃圾邮件! :-) 您需要与服务器供应商交谈以了解为什么它认为您的邮件是垃圾邮件。
标签: java email jakarta-mail yandex