user3050166
user3050166

Reputation: 1

JavaMail javax.mail.AuthenticationFailedException

I'm not familiar with this function to send mail in java.I'm getting an error while sending email to confirm a user after registered.

Below is code in TextMail :

public class TextMail extends HttpServlet {

static String sender_email = "[email protected]";
static String password = "aaaa1111";
static String host = "smtp.gmail.com";
static String port = "465";

private static class MailAuthenticator extends javax.mail.Authenticator {

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(sender_email, password);
    }
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    try {
        System.out.println("inside textmail");

        System.out.println(request.getParameter("to"));
        System.out.println(request.getParameter("text"));
        System.out.println(request.getParameter("subject"));

        String to = request.getParameter("to");
        String text = request.getParameter("text");
        String subject = request.getParameter("subject");

        Properties props = new Properties();
        props.put("mail.smtp.user", sender_email);
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        Authenticator auth = new MailAuthenticator();
        Session session = Session.getInstance(props, auth);

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(sender_email));
        message.setSubject(subject);
        message.setText(text);

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        Transport.send(message);
        System.out.println("execute textmail");
    } catch (Exception mex) {
        throw new ServletException(mex);
    }
}

the parameters for the above code is sent from another servelet :

response.sendRedirect("TextMail?to="+ul.getEmail()+"&text=path?UID="+u.getIduser()+"&subject=Comfirmation");

and these parameters passing successfully to the TextMail

Upvotes: 0

Views: 7549

Answers (1)

Giuseppe Bertone
Giuseppe Bertone

Reputation: 2244

You are mixing TLS and SSL configurations, and it's not correct. You need to choose the one to use. In particular, if you want to use SSL you should use something like this:

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");

or if you want to use TLS you can use something like this:

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.port", "587");

Please note also that you can't use your classic gmail credentials to send email via client, but you should set an application-specific password for that purpose (see https://support.google.com/accounts/answer/185833 for more informations)

For more information about configuring generic mail clients to use gmail, check https://support.google.com/mail/answer/13287

After the above considerations, you can see two working java examples at http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example

Upvotes: 2

Related Questions