C
C
ceramicthree2020-05-12 01:34:16
Java
ceramicthree, 2020-05-12 01:34:16

How to log in to SMTP mail.ru via JavaMail API?

I tried to write a simple educational application for sending letters by mail using the JavaMail API, I chose mail.ru as the SMTP server. But when I try to send a message, I get an authorization error. It seems to me that I'm not connecting to the session in the right way.
Email sending method code:

public void sendMessage(String to) {
        final String from = this.mail;
        final String password = this.password;
        String host = "smtp.mail.ru";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator(){
                    protected PasswordAuthentication getPassAuth(){
                        return new PasswordAuthentication(from, password);
                    }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("Test message");
            message.setText("This message sended by JavaMailApi");
            Transport.send(message);
            System.out.println("Success");
        }catch (MessagingException e){
            throw new RuntimeException(e);
        }

    }

String from - my mail from which I send the letter.
String password - the password for this mail.
Mistake:
Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question