邮件客户端被spamhaus阻止

时间:2016-05-11 16:01:31

标签: java email

我需要发送带有Java的电子邮件并遇到一个我似乎无法解决的问题。我收到以下错误消息:

  

javax.mail.SendFailedException:无效的地址;     嵌套异常是:       com.sun.mail.smtp.SMTPAddressFailedException:550 5.7.1服务不可用;客户端[xx.xx.xx.xx]使用zen.spamhaus.org阻止

我不明白的部分是,掩盖为xx.xx.xx.xx的IP地址实际上是我的开发计算机公共IP地址,并且没有连接到我以任何方式使用的邮件服务器。

邮件服务器是我自己的,我能够通过我尝试的所有其他渠道发送邮件(node.js应用程序,电子邮件客户端......),服务器不在任何阻止名单。

我使用的代码如下:

public class SendMailExample {

    public void sendMail(String smtpHost,String username,String password,String senderAddress,String recipientsAddress,String subject,String text ){
        MailAuthenticator auth = new MailAuthenticator(username, password);
        Properties properties = new Properties();
        properties.put("mail.smtp.host", smtpHost);
        properties.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(properties, auth);

        try {
            Message msg = new MimeMessage(session);

            msg.setFrom(new InternetAddress(senderAddress));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
                    recipientsAddress, false));
            msg.setSubject(subject);
            msg.setText(text);
            msg.setHeader("Test", "Test");
            msg.setSentDate(new Date( ));
            Transport.send(msg);

        }
        catch (Exception e) {
            e.printStackTrace( );
        }
    }

    class MailAuthenticator extends Authenticator {
        // more code - I think unrelated 
    }

    public static void main(String[] args) {
        String username = "xxx@xxx.com";
        String password = "pass";
        String senderAddress ="xxx@xxx.com";
        String recipientsAddress = "some@one.com"; 
        String subject = "Test";
        String text = "text";
        String smtpHost = "smtp.myMailServer.com";
        new SendMailExample().sendMail(smtpHost, username, password, senderAddress, recipientsAddress, subject, text);
    }
}

1 个答案:

答案 0 :(得分:0)

通过更改以下内容解决了这个问题:

添加了:

properties.put("mail.smtp.starttls.enable", "true");

到属性部分而不是

Transport.send(msg);

我现在正在使用

Transport transport = session.getTransport("smtp");
transport.connect(smtpHost, 587, username, password);
msg.saveChanges();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();