【问题标题】:trying to send emails in java using javamail尝试使用 javamail 在 java 中发送电子邮件
【发布时间】:2015-08-10 05:11:57
【问题描述】:

我正在尝试向一个电子邮件帐户 (gmail) 发送电子邮件,我已经为我不在家时使用 javamail 设置了动态提醒,但是我使用的所有示例代码都可以正确编译,但是当我使用时给我一个错误尝试运行代码(javax.mail.AuthenticationFailedException)。这里我提供了一段我目前正在使用的代码。

public static void main(String[] args) {

    final String username = "username@gmail.com";
    final String password = "password";

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

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from-email@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("to-email@gmail.com"));
        message.setSubject("Sample Subject");
        message.setText("Sample body");

        Transport.send(message);

        System.out.println("Done");

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

【问题讨论】:

    标签: java email jakarta-mail send


    【解决方案1】:

    当 Gmail 禁止“不安全的应用”访问时,有时会发生这种情况。

    你可以试试这个:登录到 gmail 网络应用程序并打开这个url 并选择“激活”选项。

    my post你可以看到详情

    【讨论】:

      【解决方案2】:

      也许您的 GMail 帐户有两步验证设置?尝试禁用它。你可以做到here

      【讨论】:

        猜你喜欢
        • 2022-01-18
        • 2018-12-18
        • 2012-03-03
        • 2012-01-28
        • 2012-04-27
        • 2017-10-17
        • 2012-03-16
        • 2014-07-26
        相关资源
        最近更新 更多