Cannot send email from within JBoss on EC2: Could not convert socket to TLS

0

Hi, I'm running a java app in JBoss 6.4.0 in a red hat 8 EC2 instance. When my app tries to send an email via javax.mail I'm getting an error "Could not convert socket to TLS".

I then coded up the AmazonSESSample.java sample program and tried it. I ran it in my EC2 instance outside JBoss and it ran successfully. (The AmazonSESSample program can be found here: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-smtp.html)

Then I commented out the email code in my java app, and replaced it with the code in AmazonSESSample.java. When I run my java app with the AmazonSESSample code in JBoss I get the same error: "Could not convert socket to TLS". So the AmazonSESSample works fine outside JBoss, and gives an error when running inside JBoss.

Here is the AmazonSESSample code in my app. Can somebody help me fix the "Could not convert socket to TLS" error?

Also I wasn't sure if I posted this in the right forum. Please suggest a better on if this one is not appropriate.

public class AmazonSESSample {

private static final Logger logger = LogManager.getFormatterLogger("AmazonSESSample");

// Replace sender@example.com with your "From" address.
// This address must be verified.
static final String FROM = "email1@gmail.com";
static final String FROMNAME = "Steve";

// Replace recipient@example.com with a "To" address. If your account 
// is still in the sandbox, this address must be verified.
static final String TO = "email2@gmail.com";

// Replace smtp_username with your Amazon SES SMTP user name.
static final String SMTP_USERNAME = "thisIsNotActualghijikl";

// Replace smtp_password with your Amazon SES SMTP password.
static final String SMTP_PASSWORD = "abcdefThisIsNotActual";

// Amazon SES SMTP host name. This example uses the US West (Oregon) region.
// See https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-endpoints
// for more information.
static final String HOST = "email-smtp.us-east-2.amazonaws.com"; 

// The port you will connect to on the Amazon SES SMTP endpoint. 
static final int PORT = 587;

static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";

static final String BODY = String.join(
	    System.getProperty("line.separator"),
	    "<h1>Amazon SES SMTP Email Test</h1>",
	    "<p>This email was sent with Amazon SES using the ", 
	    "<a href='https://github.com/javaee/javamail'>Javamail Package</a>",
	    " for <a href='https://www.java.com'>Java</a>."
	);

public int sendEmail(DisplayEmailMessage emailMessage) throws UnsupportedEncodingException, MessagingException {
	 // Create a Properties object to contain connection configuration information.
	Properties props = System.getProperties();
	props.put("mail.transport.protocol", "smtp");
	props.put("mail.smtp.port", PORT); 
	props.put("mail.smtp.starttls.enable", "true");
	props.put("mail.smtp.auth", "true");

    // Create a Session object to represent a mail session with the specified properties. 
	Session session = Session.getDefaultInstance(props);

    // Create a message with the specified information. 
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(FROM, FROMNAME));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
    msg.setSubject(SUBJECT);
    msg.setContent(BODY, "text/html");
        
    // Create a transport.
    Transport transport = session.getTransport();
                
    // Send the message.
    try {
        System.out.println("Sending...");
        
        // Connect to Amazon SES using the SMTP username and password you specified above.
        transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
    	
        // Send the email.
        transport.sendMessage(msg, msg.getAllRecipients());
        System.out.println("Email sent!");
    }
    catch (Exception ex) {
        System.out.println("The email was not sent.");
        System.out.println("Error message: " + ex.getMessage());
    }
    finally {
        // Close and terminate the connection.
        transport.close();
    }
    return 0;
}

}

Steve L
asked 2 years ago870 views
1 Answer
0

If you aren't sure if your local trust store includes the root of SES's cert chain, try adding:

props.put("mail.smtp.ssl.trust", "*");

To temporary accept all signers. If that fixes the issue you can import the SES root into the store.

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions