Mail Session fails in Tomee Server

148 Views Asked by At

I can send an email via a simple Java programme but if I use exactly the same settings to create Java Mail Session resource in Tomee's conf/tomee.xml file, the code fails:

Here is the simple Java code that WORKS:

public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable","true");
        // smtp.gmail.com supports TLSv1.2 and TLSv1.3
        // smtp.office365.com supports only TLSv1.2
        props.put("mail.smtp.ssl.protocols", "TLSv1.2");
        props.put("mail.smtp.host", "smtp.privateemail.com");
        props.put("mail.smtp.port", "587");
        
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("<valid email>", "<valid password>");
            }
        });
        
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(<valid email>));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(<valid recipient email>));
            message.setSubject("You got mail");
            message.setText("This email was sent with JavaMail.");
            Transport.send(message);
            System.out.println("Email sent.");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

However, if I try to configure the same properties in Tomee's conf/tomee.xml file, nothing works and the application throws an error:

Send failure (javax.mail.AuthenticationFailedException: null)

Here is the configuration in conf/tomee.xml

<Resource id="mail/bjm" type="javax.mail.Session">
        mail.smtp.host=smtp.privateemail.com
        mail.smtp.port=587
        mail.transport.protocol=smtp
        mail.smtp.auth=true
        ail.smtp.starttls.enable=true
        mail.smtp.ssl.protocols=TLSv1.2
        mail.smtp.user=<valid email>
        password=<valid password>
</Resource>

The configuration settings were copied from the Tomee docs that DOES NOT WORK.

And finally here is my Java/EE code that uses this resource in vain:

@Resource(name = "mail/bjm")
Session session;
...
...
MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(<valid email>));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(<valid recipient email>));

Would be grateful if someone can shed some light on the issue.

Many Thanks

0

There are 0 best solutions below