2

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.

Mr. Kevin
  • 327
  • 4
  • 12
  • Get rid of the socket factory property, [you don't need it](https://javaee.github.io/javamail/FAQ#commonmistakes). You also don't need the setHeader call since the setText call will do that for you. What version of JavaMail are you using? Your server believes you're using the wrong user name or password. Obviously you disagree. Why? – Bill Shannon Mar 26 '18 at 22:28
  • @BillShannon It says that the message that I'm sending is spam and it's being rejected automatically. Don't know how to fix it – Mr. Kevin Mar 27 '18 at 11:24
  • "Authentication failed" doesn't mean the message is spam. How can it tell if the message is spam if it fails before it even sees the message? If you're no longer getting "authentication failed" and it's failing for a different reason that says it's rejecting the message because it's spam, then stop sending spam! :-) You'll need to talk to the server vendor to understand why it thinks your message is spam. – Bill Shannon Mar 27 '18 at 16:43

2 Answers2

0
        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);
harun ugur
  • 1,718
  • 18
  • 18
-1

This could happen even if your user and password were correct. In Google there's something called "less secure apps", search for it and turn it on. I believe there should be something similar in Yandex.

S.A.
  • 99
  • 3