【问题标题】:Sending mail programatically is not working以编程方式发送电子邮件不起作用
【发布时间】:2014-12-10 05:17:13
【问题描述】:

为了发送邮件,我编写了以下代码。
以前它可以工作,但现在它不再工作了。
有人可以帮我解决我的问题吗?

public void send(String to, String subject, String msg,final String user,
            final String pass) {

    Properties props = new Properties();

    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getInstance(props,new GmailAuthenticator(user, pass));

    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

        String msgBody = GetUserPassword(userid);
        message.setSubject(subject);
        message.setText(msg);

        // 3rd step)send message
        Transport.send(message);

        System.out.println("Done");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

【问题讨论】:

  • 任何异常或not working是什么意思?
  • 它抛出一个异常说 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
  • 请将整个堆栈跟踪添加到您的问题中。

标签: java gmail-api


【解决方案1】:

您可能必须在 Gmail 中使用新的“启用安全性较低的应用”设置。使用 oauth2 令牌(使用 SMTP MSA 或 Gmail REST API)比使用密码存储 + 更可取,并且不需要在 Gmail 中禁用安全检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多