G
Guest
I have the following Perl code that works for sending email from a
Linux machine:
open(SENDMAIL, "|/usr/sbin/sendmail -t") or die "Unable to open
sendmail";
print(SENDMAIL "To: $recipients\n");
print(SENDMAIL "Subject: Test Results\n");
print(SENDMAIL "\n");
print(SENDMAIL "here are the results\n");
close(SENDMAIL);
I want to do the same thing in Java, but when I try to open that file,
I get "java.io.FileNotFoundException: !/usr/sbin/sendmail -t (No such
file or directory)"
So then I tried to use the JavaMail API, and I got the following
exception:
send failed, exception: javax.mail.SendFailedException: Sending
failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP
host: 10.11.8.45, port: 25;
nested exception is:
java.net.ConnectException: Connection refused
I did a ps -fea and I see that sendmail is running and accepting
connections. netstat -an shows that port 25 is listening. Iptables
is not running so it isn't a firewall issue. I'm trying to run the
Java program from the same machine where sendmail is running. Any
idea why JavaMail can't connect?
This is the code I'm using:
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "10.11.8.45");
props.put("mail.from", "sender@domain");
Session session = Session.getInstance(props, null);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
"(e-mail address removed)");
msg.setSubject("JavaMail hello world example");
msg.setSentDate(new java.util.Date());
msg.setText("Hello, world!\n");
Transport.send(msg);
}
catch (MessagingException mex) {
System.out.println("send failed, exception: " + mex);
}
Linux machine:
open(SENDMAIL, "|/usr/sbin/sendmail -t") or die "Unable to open
sendmail";
print(SENDMAIL "To: $recipients\n");
print(SENDMAIL "Subject: Test Results\n");
print(SENDMAIL "\n");
print(SENDMAIL "here are the results\n");
close(SENDMAIL);
I want to do the same thing in Java, but when I try to open that file,
I get "java.io.FileNotFoundException: !/usr/sbin/sendmail -t (No such
file or directory)"
So then I tried to use the JavaMail API, and I got the following
exception:
send failed, exception: javax.mail.SendFailedException: Sending
failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP
host: 10.11.8.45, port: 25;
nested exception is:
java.net.ConnectException: Connection refused
I did a ps -fea and I see that sendmail is running and accepting
connections. netstat -an shows that port 25 is listening. Iptables
is not running so it isn't a firewall issue. I'm trying to run the
Java program from the same machine where sendmail is running. Any
idea why JavaMail can't connect?
This is the code I'm using:
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "10.11.8.45");
props.put("mail.from", "sender@domain");
Session session = Session.getInstance(props, null);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
"(e-mail address removed)");
msg.setSubject("JavaMail hello world example");
msg.setSentDate(new java.util.Date());
msg.setText("Hello, world!\n");
Transport.send(msg);
}
catch (MessagingException mex) {
System.out.println("send failed, exception: " + mex);
}