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
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…