11

I am sending bulk emails using amazon ses. My code is given below

public void sendMail(String sender, LinkedList<String> recipients, String subject, String body) {
    Destination destination = new Destination(recipients);
    try {
        ACCESS_KEY = EmailSender.prop.getProperty("accessKey");
        SECRET_KEY = EmailSender.prop.getProperty("secretKey");

        Content subjectContent = new Content(subject);
        Content bodyContent = new Content(body);
        Body msgBody = new Body(bodyContent);
        Message msg = new Message(subjectContent, msgBody);

        SendEmailRequest request = new SendEmailRequest(sender, destination, msg);

        AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        AmazonSimpleEmailServiceClient sesClient = new AmazonSimpleEmailServiceClient(credentials);
        SendEmailResult result = sesClient.sendEmail(request);

        System.out.println(result + "Email sent");  
    }catch(Exception e) {
        System.out.println("Exception from EmailSender.java. Email not send");
    }

Here I have given my html content as string to the variable "body".

The mail sent successfully. But I got the html content as email. How to send html content in mail. What changes in the code will solve this issue?

Neeraj
  • 1,612
  • 7
  • 29
  • 47
  • What does the received email look like? What do you mean by "I got the html content as email"? What email client are you viewing the mail in? –  Dec 10 '12 at 13:19
  • I got mail as ..... I mean the original html code full of tags – Neeraj Dec 10 '12 at 13:30
  • What email client are you using? –  Dec 10 '12 at 13:30
  • I am doing this as programatically using amazon java sdk – Neeraj Dec 10 '12 at 13:32
  • What email client are you using to *view* the received email in? If you're just viewing the text content of the email it will show as HTML markup, and if you're using a client that can render HTML, then you will see the expected result, assuming you've done everything properly on the sending side. –  Dec 10 '12 at 13:34

1 Answers1

28

From the Amazon SES developerGuide:

You should use the WithHtml method:

Content subjContent = new Content().withData("Test of Amazon SES");
Message msg = new Message().withSubject(subjContent);

// Include a body in both text and HTML formats
Content textContent = new Content().withData("Hello - I hope you're having a good day.");
Content htmlContent = new Content().withData("<h1>Hello - I hope you're having a good day.</h1>");
Body body = new Body().withHtml(htmlContent).withText(textContent);
msg.setBody(body);