0

Hello everyone i am trying to send an email using JavaMail and Amazon SES, this is the code I have written,

static Properties props = new Properties();

static {
    props.setProperty("mail.transport.protocol", "aws");
    props.setProperty("mail.aws.user", "userName");
    props.setProperty("mail.aws.password", "secretKey");
}

void doThis() throws AddressException, MessagingException {
    Session session = Session.getDefaultInstance(props);

    Message mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress("support@xyz.com"));
    mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse("kaustubh@xyz.com"));
    mimeMessage.setSubject("Subject");
    mimeMessage.setContent("Message contenet", "text/html");

    Transport t = new AWSJavaMailTransport(session, null);
    t.connect();
    t.sendMessage(mimeMessage, null);
    t.close();
}

but i am getting an exception saying,

Exception in thread "main" javax.mail.SendFailedException: Unable to send email; nested exception is: com.amazonaws.services.simpleemail.model.MessageRejectedException: Email address is not verified. The following identities failed the check in region US-EAST-1

And I am not getting any solution for this, any suggestions from the stackOverflow family would be a great help.

Kaustubh
  • 1
  • 1
  • 1
    Does this answer your question? [Send Test Email fails with Email address is not verified](https://stackoverflow.com/questions/37528301/send-test-email-fails-with-email-address-is-not-verified) – madhead May 11 '20 at 13:22

2 Answers2

0

Here is V2 SES code that sends email...

public class SendMessage {

    // This value is set as an input parameter
    private static String SENDER = "";

    // This value is set as an input parameter
    private static String RECIPIENT = "";

    // This value is set as an input parameter
    private static String SUBJECT = "";


    // The email body for recipients with non-HTML email clients
    private static String BODY_TEXT = "Hello,\r\n" + "Here is a list of customers to contact.";

    // The HTML body of the email
    private static String BODY_HTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>"
            + "<p>Here is a list of customers to contact.</p>" + "</body>" + "</html>";

    public static void main(String[] args) throws IOException {

        if (args.length < 3) {
            System.out.println("Please specify a sender email address, a recipient email address, and a subject line");
            System.exit(1);
        }

        // snippet-start:[ses.java2.sendmessage.main]
        SENDER = args[0];
        RECIPIENT = args[1];
        SUBJECT = args[2];

        try {
            send();

        } catch (IOException | MessagingException e) {
            e.getStackTrace();
        }
    }

    public static void send() throws AddressException, MessagingException, IOException {

        Session session = Session.getDefaultInstance(new Properties());

        // Create a new MimeMessage object
        MimeMessage message = new MimeMessage(session);

        // Add subject, from and to lines
        message.setSubject(SUBJECT, "UTF-8");
        message.setFrom(new InternetAddress(SENDER));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(RECIPIENT));

        // Create a multipart/alternative child container
        MimeMultipart msgBody = new MimeMultipart("alternative");

        // Create a wrapper for the HTML and text parts
        MimeBodyPart wrap = new MimeBodyPart();

        // Define the text part
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent(BODY_TEXT, "text/plain; charset=UTF-8");

        // Define the HTML part
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(BODY_HTML, "text/html; charset=UTF-8");

        // Add the text and HTML parts to the child container
        msgBody.addBodyPart(textPart);
        msgBody.addBodyPart(htmlPart);

        // Add the child container to the wrapper object
        wrap.setContent(msgBody);

        // Create a multipart/mixed parent container
        MimeMultipart msg = new MimeMultipart("mixed");

        // Add the parent container to the message
        message.setContent(msg);

        // Add the multipart/alternative part to the message
        msg.addBodyPart(wrap);


        try {
            System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");

            Region region = Region.US_WEST_2;

            SesClient client = SesClient.builder().region(region).build();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            message.writeTo(outputStream);

            ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());

            byte[] arr = new byte[buf.remaining()];
            buf.get(arr);

            SdkBytes data = SdkBytes.fromByteArray(arr);

            RawMessage rawMessage = RawMessage.builder()
                    .data(data)
                    .build();

            SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
                    .rawMessage(rawMessage)
                    .build();

            client.sendRawEmail(rawEmailRequest);

        } catch (SdkException e) {
            e.getStackTrace();
        }
        // snippet-end:[ses.java2.sendmessage.main]
    }
}
// snippet-end:[ses.java2.sendmessage.complete]
smac2020
  • 9,637
  • 4
  • 24
  • 38
0

Exception in thread "main" javax.mail.SendFailedException: Unable to send email; nested exception is: com.amazonaws.services.simpleemail.model.MessageRejectedException: Email address is not verified. The following identities failed the check in region US-EAST-1

Amazon is telling you that you must verify the email address you're sending from or to. If the email address from and to are not verified the send email fails.

Aliou
  • 21
  • 2