gpt4 book ai didi

java - 如何解决: Sending the email to the following server failed SocketException: Permission denied: connect

转载 作者:行者123 更新时间:2023-12-01 10:08:37 26 4
gpt4 key购买 nike

我在使用 Java 以编程方式发送邮件时遇到问题。我从网络团队确认,使用防火墙通过 Java 发送邮件被阻止。但我能够在 Windows 命令提示符中从 telnet 获取响应。请在下面找到详细信息。

用于使用 Javax Mail 发送邮件的代码:

public static void main(String[] args) throws MessagingException {
String host = "mailhost.xxx";
String to = "abc@xyz.edu";
String from = "cde@xyz.edu";
String subject = "test";
String messageText = "body test";

Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "25");

// If using authentication, otherwise comment out
props.put("mail.smtp.auth", "true");

// Gmail requires TLS, your server may not
props.put("mail.smtp.starttls.enable", "true");

// Session mailSession = Session.getDefaultInstance(props, null);

Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

Transport transport = mailSession.getTransport("smtp");

// connect with authentication
// transport.connect(host,"myUsername" , "myPassword");

// connect without authentication
transport.connect();
transport.sendMessage(msg, address);

transport.close();

System.out.println("Mail was sent to " + to);
System.out.println(" from " + from);
System.out.println(" using host " + host + ".");

}

使用 Telnet 我能够在命令提示符下发送邮件:telnet 邮件主机.xxx 25我收到的回复如下:

220 mailhost Microsoft ESMTP MAIL Service ready at Tue, 29 Mar 201
6 23:34:36 -0700

谷歌搜索后,我还尝试在 Eclipse 中设置 JVM 参数,如下所示,但不幸的是仍然出现如下所示的异常

-Djava.net.preferIPv4Stack=true

异常(exception):

Exception in thread "main" org.apache.commons.mail.EmailException: Sending the email to the following server failed : mailhost.xxx:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
at org.apache.commons.mail.Email.send(Email.java:1267)
at edu.xxx.pageobject.appcenter.util.ReportGenerator.main(ReportGenerator.java:328)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: mailhost xxx, port: 25;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:297)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
... 2 more
Caused by: java.net.SocketException: Permission denied: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 9 more

请告诉我解决上述问题的任何可能的解决方案。

最佳答案

自:

  • 源异常指向权限问题,
  • 但您可以使用 telnet 从命令行进行连接

这不是网络防火墙问题。

您还排除了相关的Java 7 bug 。因此,问题可能来自您的本地 Windows 防火墙,或您正在使用的任何防病毒/防火墙。它可能会授予 telnet 权限,但不会授予 Java 权限。

尝试禁用所有本地防火墙/防病毒软件,并检查 java 是否可以成功连接。如果是这样,请重新启用防火墙,并创建异常(exception)规则以允许 Java 应用程序连接到端口 25。

关于java - 如何解决: Sending the email to the following server failed SocketException: Permission denied: connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36301545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com