I'm using a pre-configured Postfix server and interacting with it using JavaMail. I am trying to send email notifications to any external domain that may request it.
Here is the java method to send a test email:
public void TestEmail() {
String to = "[email protected]";
String from = "[email protected]";
final String username = "user";
final String password = "password";
String host = "xxx.xxx.xxx.xxx";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("Test Email");
message.setContent("This is a test email", "text/html");
Transport.send(message);
System.out.println("The test message was sent!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
When I run the method that contains the JavaMail code, I get
generalError: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 454 4.7.1 <[email protected]>: Relay access denied
I was able to test outgoing mail from the Postfix server using
echo "This is the body" | mail -s "This is the subject" [email protected]
and did receive the email in the testing inbox.
I'm not 100% sure what all the settings are on the Postfix server, but I am suspecting that is where the issue is, but I'm not familiar enough with it to start digging around and changing things all willy-nilly.
EDIT:
I removed the Authenticator()
set up from the Session creation and replaced Transport.send()
with recommended code block
Transport transport = session.getTransport("smtp");
transport.connect(host, 587, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
The entire new set of code is:
String host = "xxx.xxx.xxx.xxx";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, null);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("Test Email");
message.setContent("This is a test email", "text/html");
Transport transport = session.getTransport("smtp");
transport.connect(host, 587, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("The test message was sent!");
} catch (MessagingException e) {
System.out.println("Error: " + e);
e.printStackTrace();
throw new RuntimeException(e);
}
}
This has changed the error message I am receiving to: Unrecognized SSL message, plaintext connection?
Solution: The Java code was not the issue. The Postfix server was not configured to accept emails originating from a non-local IP/host. The IPs were added to the main.cf in the mynetworks variable.
This article has more information.
First, get rid of the Authenticator as described in the JavaMail FAQ. That will ensure that it's really trying to authenticate to the server. Turn on JavaMail session debugging to make sure it is authenticating.
Note that you can replace
Transport transport = session.getTransport("smtp");
transport.connect(host, 587, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
with
Transport.send(message, username, password);
You can get rid of the mail.smtp.auth
setting, but keep the other properties.
Make sure you're using the most current version of JavaMail.
If it still doesn't work, the article above may provide some clues as to how to change the Postfix configuration.