Ganesh Sudarsan
Ganesh Sudarsan

Reputation: 1

Java Mail API is not working?

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSender {

    private Session session;
    private final String username;
    private final String password;

    public MailSender(String username, String password) {
        this.username = username;
        this.password = password;
        init(username, password);
    }

    public final void init(String username, String password) {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MailSender.this.username, MailSender.this.password);
            }
        });

    }

    public boolean send(String recipient, String subject, String body) {
        boolean status = false;
        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message);
            status = true;

        } catch (MessagingException ex) {
            Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
        }
        return status;
    }

    public static void main(String[] args) {
        MailSender mailer = new MailSender("[email protected]", "******");
        //Userprojects u=new Userprojects();
        boolean status = mailer.send("[email protected]", "Testing Subject", "Testing message");
        System.out.println(status);
    }
}

Exception:

I have used correct login Email ID and Password to compile & execute this program by using JavamailAPI and my runabble batch file commands are given below

Authenitication failed exception : 534.5.7.14 https://account.google.com/continousSignIn

Upvotes: 0

Views: 2541

Answers (1)

Sandeep Chatterjee
Sandeep Chatterjee

Reputation: 3249

The code ran fine here.

I think you have provided an incorrect user name and/or password. If you have enabled two-way authentication, please also ensure that you are providing the correct Application Specific Password.

Unrelated to your problem - The parameters used in the init method are never used.

So you can either:

  • Removed the arguments from your init method, i.e, init();

  • Pass MailSender instance to init method, i.e, init(this);

i.e,

public final void init() {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    session = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(MailSender.this.username, MailSender.this.password);
        }
    });
}

or

public final void init(MailSender mailSender) {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    session = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(mailSender.username, mailSender.password);
        }
    });
}

Upvotes: 3

Related Questions