John John - 1 year ago 199
Java Question

JavaMail API to iMail -- Permission denied: connect

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

" 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

Here is the code that sends the e-mail. I apologize if it is a little messy, only second Java program I have tried.

Source Code:

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

Properties props = System.getProperties();
props.put("", 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", "");
props.put("mail.smtp.socketFactory.fallback", "false");

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

MimeMessage message = new MimeMessage(session);
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();
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));

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

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

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 "", port 25, isSSL false
Could not connect to SMTP host:, port: 25 Permission denied: connect

Answer Source

Add 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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download