Search code examples
javagmailjakarta-mail

Java send gmail


I'm dealing with problem sending gmail's from my app, i have following code:

public class Notificator {

public static void main(String[] args) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("r","");
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler," +
                "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

}

And im getting following error : Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: failed to connect

Threfore I enabled access for less secured devices and also enabled intellij in firewall.

edit: Yes i have correct credintials, just erased them before putting here.


Solution

  • You need to specify the gmail account details (email address and password) or you will always receive an Authorisation error.

    Here's a sample which i have used previously:

        public JavaMailSender getJavaMailSender() {
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    
            javaMailSender.setHost("smtp.gmail.com");
            javaMailSender.setPort(587);
            javaMailSender.setUsername("[email protected]");
            javaMailSender.setPassword("GMAIL PASSWORD HERE");
    
            javaMailSender.setJavaMailProperties(getMailProperties());
            return javaMailSender;
        }
    
        private Properties getMailProperties() {
            Properties properties = new Properties();
            properties.setProperty("mail.transport.protocol", "smtp"));
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.starttls.enable", "true");
            properties.setProperty("mail.debug", "false");
            return properties;
        }
    
        JavaMailSender javaMailSender = getJavaMailSender();
    
        private void sendEmail(){
            javaMailSender.send((MimeMessage mimeMessage) -> {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                message.setFrom("[email protected]");
                message.setTo("[email protected]");
                message.setSubject("Testing Subject");
                message.setText("Dear Mail Crawler," +
                    "\n\n No spam to my email, please!");
            });
        }