我正在构建一个使用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服务的凭据。 最佳答案
String from = "[email protected]";
String pass = "yourmail_password";
String[] to = { "to_mail_address@****.com" };
String host = "smtp.yandex.com";
Properties props = System.getProperties();
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.getDefaultInstance(props, null);
关于java - JavaMail Yandex发件人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49489767/