JavaMail API到iMail - java.net.SocketException:权限被拒绝:连接 [英] JavaMail API to iMail -- java.net.SocketException: Permission denied: connect
问题描述
我无法让应用程序使用JavaMail API以比以前更自动化的方式发送一些文件。我是Java和NetBeans的新手,但是已经用其他语言编程了,所以请原谅我,如果我对Java和NetBeans有点遗失。
I am having trouble getting an application to use the JavaMail API to send out some files in a more automated way than we are used to doing. I am very new to Java and NetBeans, but have programmed in other languages, so please forgive me if I seem a little lost to Java and or NetBeans.
我一直在此错误
java.net.SocketException:权限被拒绝:连接
java.net.SocketException: Permission denied: connect
尝试连接到本地邮件服务器时。我已经通过gmail的SMTP服务器使用相同的代码成功连接和发送邮件,只需更改用户名,密码和端口。我还能够成功远程登录到我们的服务器并从端口25获得220响应。我还有一个运行的批处理文件,它通过我们的本地服务器成功发送电子邮件。有关为什么我无法通过 JavaMail
进行连接的任何想法或想法?
when trying to connect to the local mail server. I have connected and sent mail successfully through gmail's SMTP server with the same code, just changing username, password, and port. I was also able to telnet to our server successfully and get a 220 response from port 25. I also have a batch file that runs and it successfully sends e-mail through our local server. Any thoughts or ideas as to why I can't connect through JavaMail
?
以下是发送电子邮件。
Here is the code that sends the e-mail.
源代码:
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
推荐答案
将 -Djava.net.preferIPv4Stack = true
添加到VM选项。另一种确认是否是同一问题的方法,在Netbeans中,右键单击项目>属性>库并选择JDK 6 Java平台(如果没有,请安装)。清洁,构建然后再试一次。这将消除此问题作为问题
Add -Djava.net.preferIPv4Stack=true
to the VM options. Another way to confirm if it is the same issue, in Netbeans, right click on the project > properties > Libraries and choose a JDK 6 Java Platform (install if you do not have it). Clean, build then try again. This will eliminate this issue as the problem
信用 https:// stackoverflow。 com / a / 7478027/643500
这篇关于JavaMail API到iMail - java.net.SocketException:权限被拒绝:连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!