簡體   English   中英

無法使用Java發送郵件

[英]Can't Send Mail in Java

我試圖用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);

通過將“ 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

試試這個配置

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。

您可以輕松地從Gmail發送郵件。 逐步說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM