JavaMail连接问题 [英] JavaMail connection problems

查看:253
本文介绍了JavaMail连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试一个在线发现的简单程序,以便使用JavaMail发送电子邮件。我试图使用工作电子邮件,但我收到错误无法连接到SMTP主机:...和权限被拒绝:连接。我已查看过有关此问题的其他帖子,包括:



JavaMail Exchange身份验证



使用JSP发送电子邮件



JavaMail API to iMail-java.net.SocketException:Permission denied:connect



<我认为我已经解决了那些基本上是IPv4问题和身份验证的帖子解决方案中提到的问题。我是尝试使用JavaMail的新手,所以我想知道我是否在制作其他初学者错误。还有其他我忽略的事情吗?我是否有可能以这种方式访问​​服务器?我使用的是通用名称,而不是我公司的实际名称。



代码如下:

  public static void main(String [] args)
{
System.setProperty(java.net.preferIPv4Stack,true);
String host =mail.company.com;
final String user =user@company.com; //相应地更改
final String password =XXXXXXXX; //相应地更改

String to =user @ company.com; //相应地更改

//获取会话对象
属性props = new Properties();
props.put(mail.smtp.host,host);
props.put(mail.smtp.auth,true);

会话session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(用户,密码);
}
});

//撰写邮件
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(javatpoint);
message.setText(这是使用JavaMail API发送电子邮件的简单程序);

//发送消息
Transport.send(message);

System.out.println(消息发送成功...);

}
catch(MessagingException e)
{
e.printStackTrace();
}
}

堆栈追踪:

  javax.mail.MessagingException:无法连接到SMTP主机:mail.company.com,port:25; 
嵌套异常是:
java.net.SocketException:权限被拒绝: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)

引起:java.net.SocketException:权限被拒绝:在java.net.TwoStacksPlainSocketImpl.socketConnect(本地方法)连接
java.net.AbstractPlainSocketImpl.doConnect(未知来源)
java.net.AbstractPlainSocketImpl.connectToAddress(未知来源)
b $ b at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.So cksSocketImpl.connect(未知来源)
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更多


解决方案

java.net.SocketException:权限被拒绝:连接

这意味着您已连接到服务器和端口,服务器主动拒绝您的连接。



如果您尝试将 telnet 添加到此服务器和端口,您可能会获得更具描述性的消息,无论哪种方式,端口无法正常工作。



最有可能阻止端口 25 作为安全措施,这是大多数公司的相当标准设置。



由于您使用Outlook无法在设置中找到端口,我假设您正在连接到Exchange服务器,这可能是如果你的整个公司都是Microsoft Outlook商店,甚至根本没有启用 SMTP



你需要联系你的系统管理团队找出你应该实际使用的端口和协议。


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 Exchange Authentication

Sending email using JSP

JavaMail API to iMail -- java.net.SocketException: Permission denied: connect

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.

The code is below:

public static void main(String[] args) 
{   
    System.setProperty("java.net.preferIPv4Stack" , "true");
    String host="mail.company.com";   
    final String user="user@company.com";//change accordingly   
    final String password="XXXXXXXX";//change accordingly   

    String to="user@company.com";//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

解决方案

java.net.SocketException: Permission denied: connect

This means you connected to the server and port and the server actively denied your 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.

Most likely that port 25 is blocked as a security measure, this is pretty standard settings for most companies.

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.

You will need to contact your system admin team to find out what port and protocol you should actually be using.

这篇关于JavaMail连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆