JavaMail API to iMail -- java.net.SocketException: Permission denied: connect
jakarta-mail, java, permission-denied, socketexception
Solution
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
Problem
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 < 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()); } } ``` 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 ```