【问题标题】:Could not connect to SMTP host Permission denied: connect无法连接到 SMTP 主机权限被拒绝:连接
【发布时间】:2015-05-15 06:44:30
【问题描述】:

电子邮件在 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。你是我最后的希望,请帮助我。提前致谢。

【问题讨论】:

  • 您确定您的凭据正确吗?您可以将它们注销以进行验证吗?可以试试 587 端口吗?
  • @eddiezane 是的,我的凭据是正确的。我尝试了其他端口,但它无法正常工作。

标签: java email sendgrid


【解决方案1】:

我遇到了类似类型的错误,所以我喜欢这样,现在代码对我来说工作正常另外我还禁用了我系统中的所有防病毒软件,它们通常会导致这个问题。 希望它对你有用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2022-12-17
    • 1970-01-01
    相关资源
    最近更新 更多