JavaMail 连接问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24940883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaMail connection problems
提问by klib
I am trying to test a simple program found online to send an email using JavaMail. I am attempting to use a work email but I get an error "Could not connect to SMTP host:..." and "Permission denied: connect". I have looked through other posts on this issue including:
我正在尝试测试一个在网上找到的使用 JavaMail 发送电子邮件的简单程序。我正在尝试使用工作电子邮件,但收到错误“无法连接到 SMTP 主机:...”和“权限被拒绝:连接”。我浏览了有关此问题的其他帖子,包括:
JavaMail Exchange Authentication
JavaMail API to iMail -- java.net.SocketException: Permission denied: connect
JavaMail API 到 iMail -- java.net.SocketException: 权限被拒绝:连接
I think I have addressed the problems mentioned in the solutions of those posts which are basically the IPv4 issue and the authentication. I am new to attempting to using JavaMail so I wonder if I am making some other beginner mistake. Are there any other things I am overlooking? Is it possible I just do not have access to the server in this manner? I have used generic names not the actual name of my company.
我想我已经解决了那些帖子的解决方案中提到的问题,这些问题基本上是 IPv4 问题和身份验证。我是尝试使用 JavaMail 的新手,所以我想知道我是否犯了其他一些初学者错误。还有其他我忽略的东西吗?是否有可能我只是无法以这种方式访问服务器?我使用的通用名称不是我公司的实际名称。
The code is below:
代码如下:
public static void main(String[] args)
{
System.setProperty("java.net.preferIPv4Stack" , "true");
String host="mail.company.com";
final String user="[email protected]";//change accordingly
final String password="XXXXXXXX";//change accordingly
String to="[email protected]";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user,password);
}
});
//Compose the message
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("javatpoint");
message.setText("This is simple program of sending email using JavaMail API");
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
Stack Trace:
堆栈跟踪:
javax.mail.MessagingException: Could not connect to SMTP host: mail.company.com, port: 25;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at mailtesting.SendMailBySite.main(SendMailBySite.java:45)
Caused by: java.net.SocketException: Permission denied: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 7 more
采纳答案by klib
java.net.SocketException: Permission denied: connect
This means you connected to the server and port and the server actively deniedyour connection.
这意味着您连接到服务器和端口,服务器主动拒绝了您的连接。
If you try and telnet
to this server and port you might get a more descriptive message, either way, that port isn't going to work.
如果您尝试telnet
访问此服务器和端口,您可能会收到更具描述性的消息,无论哪种方式,该端口都不会工作。
Most likely that port 25
is blocked as a security measure, this is pretty standard settings for most companies.
很可能该端口25
被阻止作为安全措施,这对于大多数公司来说是非常标准的设置。
Since you are using Outlook can't find the port in the settings, I am assuming you are connecting to an Exchange server, which might not even have SMTP
enabled at all if your entire company is a Microsoft Outlook shop.
由于您使用的 Outlook 无法在设置中找到端口,我假设您正在连接到 Exchange 服务器,SMTP
如果您的整个公司都是 Microsoft Outlook 商店,则该服务器甚至可能根本没有启用。