【问题标题】:Unable to send email using servlet and JavaMail无法使用 servlet 和 JavaMail 发送电子邮件
【发布时间】:2019-08-10 23:38:04
【问题描述】:

我正在使用JavaMail 发送电子邮件,但收到javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials g188sm3298732pfc.24 - gsmtp 的错误

  • 我正在做的是,我有一个类为EmailUtility
  • 上面的类有一个静态方法,sendEmail()——它以 SMTP 服务器设置和消息细节作为它的参数。
  • 我将 SMTP 服务器设置放入我的 web.xml 文件中 但不知道怎么回事

我的EmailUtility 班级

public class EmailUtility {
public static void sendEmail(String host, String port, final String userName, final String password,
        String toAddress, String subject, String message) throws AddressException, MessagingException {

    // setting SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    });
    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(message);

    // sending the e-mail
    Transport.send(msg);

}

}

这是我的 web.xml 文件

 <context-param>
    <param-name>host</param-name>
    <param-value>smtp.gmail.com</param-value>
</context-param>

<context-param>
    <param-name>port</param-name>
    <param-value>587</param-value>
</context-param>

<context-param>
    <param-name>user</param-name>
    <param-value>test.123@gmail.com</param-value>
</context-param>
<context-param>
    <param-name>pass</param-name>
    <param-value>12345698</param-value>
</context-param>

这是我的 servlet 类

public void init() {
    // reading SMTP server setting from web.xml file
    ServletContext context = getServletContext();
    host = context.getInitParameter("host");
    port = context.getInitParameter("port");
    user = context.getInitParameter("user");
    pass = context.getInitParameter("pass");
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // reading form fields
    String recipient = request.getParameter("recipient");
    String subject = request.getParameter("subject");
    String content = request.getParameter("content");
    System.out.println(recipient+" sub "+subject+" content "+content);

    String resultMessage = "";

    try {
        EmailUtility.sendEmail(host, port, user, pass, recipient, subject,
                content);
        resultMessage = "The e-mail was sent successfully";
    } catch (Exception ex) {
        ex.printStackTrace();
        resultMessage = "There were an error: " + ex.getMessage();
    }

我做对了,但不知道问题出在哪里

如果我在 gmail 中启用安全性较低的应用设置,那么它工作正常,我认为这不是解决问题的正确方法,因为并非每个用户都会这样做

所以大家帮帮我,我不知道我在做什么奇怪,谢谢

编辑是否有任何其他资源可以让我在跨平台(例如从 gmail 到 yahoo)上发送邮件,我愿意使用任何其他可以完成我正在尝试的任务的资源

【问题讨论】:

标签: java email security jakarta-mail


【解决方案1】:

不同的服务器有不同的安全要求。如果你想处理所有这些,你将需要在你的应用程序中使用一些特殊情况。

有时您需要用户为电子邮件服务器创建应用专用密码。

如果服务器支持,有时您需要支持OAuth

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 2015-01-09
    • 2014-07-26
    • 2015-08-19
    • 1970-01-01
    • 2018-12-18
    相关资源
    最近更新 更多