JavaMail API to iMail -- java.net.SocketException: Permission denyed: connect
2022-09-02 10:23:06
我很难让应用程序使用JavaMail API以比我们习惯的更自动化的方式发送一些文件。我对 Java 和 NetBeans 非常陌生,但已经用其他语言编程了,所以如果我对 Java 和/或 NetBeans 有点迷茫,请原谅我。
我一直收到此错误
java.net.SocketException: Permission deny: connect
尝试连接到本地邮件服务器时。我已经使用相同的代码通过gmail的SMTP服务器成功连接并发送邮件,只需更改用户名,密码和端口即可。我还能够成功地远程登录到我们的服务器,并从端口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