I'm building an app to send and receive emails with Yandex on Java using JavaMail. When I'm sending an email I found a problem and I don't know how to fix it. It just pops and error saying 535 5.7.8 Error: authentication failed: Invalid user or password!
.
My code is the following:
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
and pass
are the credentials to log-in into Yandex service.