无法用Java发送邮件

时间:2013-08-12 14:34:03

标签: java email tomcat smtp javamail

我正在尝试用java发送邮件,但我一直收到此错误“com.sun.mail.smtp.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);

3 个答案:

答案 0 :(得分:1)

通过将“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

答案 1 :(得分:0)

尝试此配置

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。

答案 2 :(得分:0)

您可以轻松地从Gmail发送邮件。在here

中逐步介绍