简体   繁体   English

JavaMail javax.mail.AuthenticationFailedException

[英]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. 我不熟悉用Java发送邮件的功能,注册后发送电子邮件确认用户时出现错误。

Below is code in TextMail : 下面是TextMail中的代码

public class TextMail extends HttpServlet {

static String sender_email = "abc@gmail.com";
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 : 上面代码的参数是从另一个servelet发送的

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

and these parameters passing successfully to the TextMail 并将这些参数成功传递到TextMail

You are mixing TLS and SSL configurations, and it's not correct. 您正在混合使用TLS和SSL配置,这是不正确的。 You need to choose the one to use. 您需要选择一个使用。 In particular, if you want to use SSL you should use something like this: 特别是,如果您想使用SSL,则应使用以下内容:

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: 或者,如果您想使用TLS,则可以使用以下方法:

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) 另请注意,您不能使用传统的gmail凭据通过客户端发送电子邮件,但应为此设置应用程序专用密码(有关更多信息,请参见https://support.google.com/accounts/answer/185833)信息)

For more information about configuring generic mail clients to use gmail, check https://support.google.com/mail/answer/13287 有关配置常规邮件客户端以使用gmail的更多信息,请查看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 经过上述考虑,您可以在http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example中看到两个有效的Java示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM