我正在建立一个应用程序来发送和接收使用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服务的凭证。
发布于 2019-11-22 18:54:58
String from = "your_mail_address@yandex.com";
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);发布于 2018-12-23 09:05:48
即使您的用户名和密码正确,也可能发生这种情况。在Google中有一个叫做“不太安全的应用程序”,搜索它并打开它。我相信Yandex应该有类似的东西。
https://stackoverflow.com/questions/49489767
复制相似问题