【问题标题】:Can't Send Mail in Java无法在 Java 中发送邮件
【发布时间】:2013-08-13 22:15:23
【问题描述】:

我正在尝试在 java 中发送邮件,但我不断收到此错误“com.sun.mail.smtp.SMTPSendFailedException: 550 failed to meet SPF requirements”,我已经在互联网上搜索是否有其他人遇到过这个问题java,什么也没找到。任何人都知道这个错误意味着什么?我发送电子邮件的代码如下。

            //Create session and message
            Properties props = System.getProperties();
            props.put("mail.smtp.user", user);
            props.put("mail.smtp.password", password);
            props.put("mail.smtp.auth", "false");
            props.put("mail.smtp.host", mailhost);

            javax.mail.Authenticator auth = null;
            auth = new javax.mail.Authenticator() {
                @Override
                public javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication(user, password);
                }
            };
            session = Session.getInstance(props, auth);
            Message msg = new MimeMessage(session);

           //Set from,recipients,content and other stuff here
           //...................

           //Send the message
           Transport.send(msg);

【问题讨论】:

标签: java email tomcat smtp jakarta-mail


【解决方案1】:

试试这个配置

props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.port", port);

这些收件人的邮件服务器似乎已对传入邮件实施 SPF 检查,而您的域未声明 SPF。

【讨论】:

  • 我这样做了,现在我得到了一个“javax.mail.AuthenticationFailedException”,这真是令人困惑的东西,因为 smtp 服务器上并没有真正改变。
  • 您可能需要使用 SSL 进行连接,或者在连接后使用 STARTTLS 命令切换到 SSL/TLS。尝试将“mail.smtp.ssl.enable”设置为“true”。有关您可以设置的所有属性,请参阅 com.sun.mail.smtp 包的 javadocs。当然,请确保您提供正确的密码!
【解决方案2】:

通过将“mail.smtp.auth”属性设置为true并添加属性“mail.smtp.ssl.enable”并将其设置为true然后最终而不是使用下面的静态方法来发送消息。

Transport.send(Message msg) 

我在从会话对象获得的传输对象上使用实例方法来发送消息。

transport.sendMessage(Message msg, Address[] addresses)  

以下是修改后的工作代码。

       //Create session
        Properties props = System.getProperties();
        props.put("mail.smtp.user", user);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", mailhost);
        props.put("mail.smtp.ssl.enable", "true");


        javax.mail.Authenticator auth = null;
        auth = new javax.mail.Authenticator() {
            @Override
            public javax.mail.PasswordAuthentication getPasswordAuthentication() {
                return new javax.mail.PasswordAuthentication(user, password);
            }
        };
        session = Session.getInstance(props, auth);
        //get transport object from session and connect to mail server
        Transport tr = session.getTransport("smtp");
        tr.connect(session.getProperty("mail.smtp.host"),  session.getProperty("mail.smtp.user"), session.getProperty("mail.smtp.password"));

        //create message  and set from,recipients,content and other stuff here on the message object.
        Message msg = new MimeMessage(session);
       //..................
       //...................

       //Save and send the message
        msg.saveChanges();
        tr.sendMessage(msg, msg.getAllRecipients());
        tr.close();

这个链接真的帮我解决了我的问题:http://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

【讨论】:

    【解决方案3】:

    您可以轻松地从您的 gmail 发送邮件。一步一步在here中描述

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2017-10-06
      • 2020-01-04
      • 2013-05-01
      • 2014-10-21
      相关资源
      最近更新 更多