gpt4 book ai didi

JavaMail javax.mail.AuthenticationFailedException

转载 作者:行者123 更新时间:2023-12-02 06:28:34 26 4
gpt4 key购买 nike

我不熟悉这个在java中发送邮件的功能。我在注册后发送电子邮件以确认用户时遇到错误。

下面是 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);
}
}

上述代码的参数是从另一个servlet发送的:

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

这些参数成功传递到 TextMail

最佳答案

您混合了 TLS 和 SSL 配置,这是不正确的。您需要选择要使用的一个。特别是,如果您想使用 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");

或者如果您想使用 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");

另请注意,您无法使用经典 Gmail 凭据通过客户端发送电子邮件,但您应该为此目的设置应用程序专用密码(有关详细信息,请参阅 https://support.google.com/accounts/answer/185833)

有关配置通用邮件客户端以使用 gmail 的详细信息,请查看 https://support.google.com/mail/answer/13287

经过上述考虑,您可以在 http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example 看到两个有效的 java 示例。

关于JavaMail javax.mail.AuthenticationFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20290625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com