2015-05-15 280 views
0

电子邮件在Windows Server 2008中正常工作,但当我更改为Windows Server 2012时,其导致此错误。无法连接到SMTP主机权限被拒绝:连接

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

我用Google搜索关于这一点,他们告诉-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,支柱2和Apache Tomcat 8.You是我最后的希望,请帮我这一点。提前致谢。

+0

你确定你的凭证是正确的吗?你可以注销它们来验证吗?你可以尝试端口587? – eddiezane

+0

@eddiezane是的,我的凭据是正确的。我尝试了其他港口,它不工作。 – Subash

回答

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 
+0

我使用了sendgrid的web api,它的工作正常。 – Subash

相关问题