Can';不要用Java发送邮件

Can';不要用Java发送邮件,java,email,tomcat,smtp,jakarta-mail,Java,Email,Tomcat,Smtp,Jakarta Mail,我正在尝试用java发送邮件,但我一直收到这个错误“com.sun.mail.SMTPSendFailedException:550未能满足SPF要求”,我已经在互联网上搜索,看看是否有人在java中遇到这个问题,但什么也没有找到。有人知道这个错误是什么意思吗?我发送电子邮件的代码如下 //Create session and message Properties props = System.getProperties();

我正在尝试用java发送邮件,但我一直收到这个错误“com.sun.mail.SMTPSendFailedException:550未能满足SPF要求”,我已经在互联网上搜索,看看是否有人在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);
尝试此配置

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。

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

Transport.send(Message msg) 
transport.sendMessage(Message msg, Address[] addresses)  
我在从会话对象获取的传输对象上使用实例方法来发送消息

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

这个链接确实帮助我解决了我的问题:

你可以轻松地从gmail发送邮件。有问题的SMTP服务器和目标服务器是哪个?有些需要专门的cyptographic身份验证。我这样做了,现在我得到了一个“javax.mail.AuthenticationFailedException”,这让人非常困惑,因为smtp服务器上没有任何更改。您可能需要使用SSL进行连接,或者在连接后使用STARTTLS命令切换到SSL/TLS。尝试将“mail.smtp.ssl.enable”设置为“true”。有关可以设置的所有属性,请参阅com.sun.mail.smtp包的javadocs。当然,请确保您提供了正确的密码!