无法连接到SMTP主机权限被拒绝:连接

时间:2015-05-15 06:44:31

标签: java email sendgrid

电子邮件在Windows Server 2008中正常运行,但当我更改为Windows Server 2012时,会导致此错误。

SystemException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.sendgrid.net, port: 25; nested exception is: java.net.SocketException: Permission denied: connect 

我已经搜索了这个,他们告诉-Djava.net.preferIPv4Stack = true,把这个条目放在jvm系统属性中,并要求禁用防病毒软件。两者都没有奏效。我也尝试联系ISP打开25端口,现在25端口正在监听。这是我的代码。

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.port", 25);
    props.put("mail.smtp.auth", "true");
    SMTPAuthenticator auth = new SMTPAuthenticator();
    Session mailSession = Session.getDefaultInstance(props, auth);
    mailSession.setDebug(true);
    Transport transport = mailSession.getTransport();
    MimeMessage message = new MimeMessage(mailSession);
    //read the Template and replace values using Velocity Engine
    String text = messageContent;
    message.setFrom(new InternetAddress(from));
    message.setSubject(subject);
    transport.connect();
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();

我正在使用sendgrid,java8,struts 2和apache tomcat 8.你是我最后的希望,请帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我有类似的错误,所以我喜欢这个,现在代码工作正常我加上我也禁用我的系统中的所有防病毒软件,他们通常会导致这个问题。 希望它适合你。

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");  // SMTP Host
props.put("mail.smtp.port", "587");  // TLS Port 25/587
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");  // enable authentication