I'm trying to send an email invoked from code.
@Stateless
public class MailBean {
private static Logger LOGGER = Logger.getLogger(MailBean.class);
private String EMAIL_REGEX = "^(([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}){1,25})+)*$";
@Resource(name = "java:jboss/mail/Default")
private Session mailSession;
@Asynchronous
public void send(String addresses, String topic, String textMessage) {
try {
MimeMessage message = new MimeMessage(mailSession);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addresses));
message.setSubject(topic);
message.setText(textMessage);
Transport transport = mailSession.getTransport();
transport.connect();
transport.send(message);
} catch (MessagingException ex) {
LOGGER.error("Cannot send mail to " + addresses + ". Error: " + ex.getMessage(), ex);
}
}
public boolean isValidEmailAddress(String email) {
if (email == null)
return false;
else
return email.matches(EMAIL_REGEX);
}
}
My Wildfly 8.1 Server is configured as follows:
<subsystem xmlns="urn:jboss:domain:mail:2.0">
<mail-session name="mail-session-default" jndi-name="java:jboss/mail/Default">
<smtp-server outbound-socket-binding-ref="mail-smtp"
ssl="false"
username="john@doe.com"
password="****"/>
</mail-session>
</subsystem>
The socket outbound like this:
<outbound-socket-binding name="mail-smtp">
<remote-destination host="mail.doe.com" port="25"/>
<outbound-socket-binding>
The reported error is
(EJB default - 2) L:38 Cannot send mail to jane@doe.com. Error: 550 5.7.1 Command rejected
: com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Command rejected
As in the example I try to send an email from my account john@doe.com to jane@doe.com. Not to another domain.
On startup, wildfly does not report any errors with this configuration.
[org.jboss.as.mail.extension] (MSC service thread 1-5) L:136 JBAS015400: Bound mail session [java:jboss/mail/Default]
Any clue why that fails? In general I wonder why Java-Mail behaves not like a regular mail client.