Question

In http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-java.html there is an explanation on how to send emails via AWS-SES. It refers to access_key and secret_key. But what I have is SMTP Username and SMTP Password that I have generated on the portal.

Currently my code is as follows:

AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);
client.sendEmail(request);

The constructor for AmazonSimpleEmailServiceClient takes AWSCredentials but NOT the smtp credentials. Any idea on how to use the SMTP credentials?

Was it helpful?

Solution

Use JavaMail as the transport for Amazon SES SMTP. Instructions and sample code for using the SMTP endpoint are also provided in the Amazon SES documentation.

Use the SDK if you intend to send email via the Amazon SES API.

OTHER TIPS

You can use below code to send mail through SMTP 

 InternetAddress[] parse = InternetAddress.parse(toAddess , true);

        Properties props = System.getProperties();
        //Add properties
        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "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(fromemail));


        msg.setRecipients(javax.mail.Message.RecipientType.TO,  parse);


        msg.setSubject(subject);
        msg.setContent(body,"text/html");

        // Create a transport.
        Transport transport = session.getTransport();

        // Send the message.
        try
        {
            logger.info("Attempting to send an email through the Amazon SES SMTP interface to "+toAddess);

            // Connect to Amazon SES using the SMTP username and password specified above.
            transport.connect(smtpHost, port, smtpuserName, smtpPassword);


            // Send the email.
            transport.sendMessage(msg, msg.getAllRecipients());
           logger.info("MessageID"+ msg.getMessageID());

            logger.info("Email sent!");
            return msg.getMessageID();
        }
        catch (Exception ex) {
            logger.error("The email was not sent. Error message: " + ex.getMessage());
        }
        finally
        {
            // Close and terminate the connection.
            transport.close();
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top