0

Setting up sending mail via smtp.

private String mailhost = "smtp.yandex.ru";
private String user;
private String password;
private Session session;

public static final String TAG = "GMailSender: ";

static {
    Security.addProvider(new JSSEProvider());
}

public GMailSender(String user, String password) {
    this.user = user;
    this.password = password;

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.host", mailhost);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.quitwait", "false");

    session = Session.getDefaultInstance(props, this);
}

protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(user, password);
}

I do it according to this instruction https://yandex.ru/support/mail/mail-clients/others.html. a password from passwords for applications. But I get the error Error sending mail com. sun. mail. smtp. SMTPSendFailedException:553 5.7.1 Sender address rejected: does not belong to the auth user.

Ксения
  • 67
  • 1
  • 8

2 Answers2

1

Rephrasing the error message:

The user you log in with is not authorized to send email on behalf of the email address listed in the from field of the email (the sender).

This kind of security is used to prevent you from sending spam email, pretending to be someone else.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I do not specify the sender, I only log in. If the sender specifies the same email as for logging in, will it work? How do I do this? – Ксения Jul 01 '21 at 06:40
  • @Ксения How do you do what? Specify the `from` field of an email to be sent? Depends on how you create the email (what API you use). – Andreas Jul 01 '21 at 06:44
  • yes, it works. must add message.setFrom(new InternetAddress("mymail@yandex.ru")); – Ксения Jul 01 '21 at 06:44
0

I believe there is an error on yandex side. I cannot send email as well via SMTP.

b166er
  • 19
  • 5