2012-10-15 73 views
17

我无法使应用程序使用JavaMail API以比我们以前更加自动化的方式发送某些文件。我对Java和NetBeans非常陌生,但是已经使用其他语言进行了编程,所以请原谅我,看看是否对Java和NetBeans有点迷茫。JavaMail API to iMail - java.net.SocketException:权限被拒绝:连接

我不断收到此错误

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

试图连接到本地邮件服务器时。我通过gmail的SMTP服务器使用相同的代码成功地连接并发送了邮件,只是更改了用户名,密码和端口。我也能够成功地telnet到我们的服务器,并从端口25获得220响应。我也有一个批处理文件运行,它成功地通过我们的本地服务器发送电子邮件。关于为什么我无法通过JavaMail进行连接的任何想法或想法?

这是发送电子邮件的代码。

的源代码:从两个异常声明

public void sendEmail(String customerNumber, ArrayList fileList){ 
    String from = "xxxx"; 
    String username = "xxxx"; 
    String to = "xxxx"; 
    String host = "10.1.1.6"; 
    String pwd = "xxxx"; 
    String port = "25"; 

    Properties props = System.getProperties(); 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.port", port); 
    props.put("mail.smtp.user", username); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.debug", "true"); 
    props.put("mail.smtp.socketFactory.port", port); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 

    Session session = Session.getInstance(props, null); 
    session.setDebug(true); 

    MimeMessage message = new MimeMessage(session); 
    try{ 
     message.setFrom(new InternetAddress(from)); 
     message.setRecipients(Message.RecipientType.TO, to); 
     message.setSubject("Electronic Invoices"); 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText("Electronic Invoices"); 
     Multipart multipart = new MimeMultipart(); 
     multipart.addBodyPart(messageBodyPart); 
     for(int i = 0; i < fileList.size(); i++){ 
      messageBodyPart = new MimeBodyPart(); 
      String fileName = (String) fileList.get(i); 
      DataSource source = new FileDataSource(fileName); 
      messageBodyPart.setDataHandler(new DataHandler(source)); 
      messageBodyPart.setFileName(fileName); 
      multipart.addBodyPart(messageBodyPart); 
     } 
     message.setContent(multipart); 

     Transport tr; 
     tr = session.getTransport("smtp"); 
     tr.connect(host, username, pwd); 
     tr.sendMessage(message, message.getAllRecipients()); 
     jTextArea2.append("Mail Sent Successfully"); 
     tr.close(); 

    } catch(Exception e){ 
     jTextArea2.append("sendEmail()::" + System.getProperty("line.separator") + e + System.getProperty("line.separator")); 
     System.out.println(e.getMessage()); 
     System.out.println(e.getCause()); 
    } 
} 

输出:

DEBUG: setDebug: JavaMail version 1.4.5 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: trying to connect to host "10.1.1.6", port 25, isSSL false 
Could not connect to SMTP host: 10.1.1.6, port: 25 
java.net.SocketException: Permission denied: connect 
+0

JDK 7?检查这个人http://stackoverflow.com/a/7478027/643500 –

+0

我添加了-Djava.net.preferIPv4Stack = true到我的NetBeans配置文件,但我仍然有错误。它仍然允许我从Gmail帐户发送。感谢您的快速回复。 – John

+1

这应该去VM选项。在Netbeans中,另一种确认是否属于同一问题的方法是,右键单击项目>属性>库并选择一个JDK 6 Java平台(如果没有它,请安装)。清理,建立然后再试一次。这将消除这个问题作为问题。 –

回答

18

添加-Djava.net.preferIPv4Stack=true到VM选项。在Netbeans中,另一种确认是否属于同一问题的方法是,右键单击项目>属性>库并选择一个JDK 6 Java平台(如果没有它,请安装)。清理,建立然后再试一次。这将消除这个问题,因为这个问题

信用https://stackoverflow.com/a/7478027/643500

1

如果简化在启动代码中使用的应用程序调用(例如,从CLI):

System.setProperty("java.net.preferIPv4Stack", "true")

假定应用程序将正在使用传统的IPv4网络堆栈。

相关问题