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

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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.

I keep getting this error

java.net.SocketException: Permission denied: connect

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.

Source code:

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 

Output from the two Exception statements:

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 

回答1:

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

Credit https://stackoverflow.com/a/7478027/643500



回答2:

In case to simplify app invocation (e.g. from CLI) used in the code at start:

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

supposing that the app will be working with legacy IPv4 network stack.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!