vendredi 1 mai 2015

How to include attachment in AWS SES Email

Is anyone already tried using SES Email in AWS. The email code from Amazon is working but I want to include the PDF attachment. Please help, how can I include the attachment as part of the email.

JSONArray sendingemail = new JSONArray();

    final String FROM = "fromemail@gmail.com";  // Replace with your "From" address. This address must be verified.
    final String TO = "toemail@gmail.com"; // Replace with a "To" address. If your account is still in the
                                                      // sandbox, this address must be verified.
    final String BODY = "This email was sent through Amazon SES by using the AWS SDK for Java.";
    final String SUBJECT = "Amazon SES test (AWS SDK for Java)";
    Destination destination = new Destination().withToAddresses(new String[]{TO});
    FileOutputStream filename = new FileOutputStream("AddTableExample.pdf");

    // Create the subject and body of the message.
    Content subject = new Content().withData(SUBJECT);
    Content textBody = new Content().withData(BODY); 

    Attachment attach = new Attachment().withFileName(filename.toString());

    // Create a message with the specified subject and body.
    Body body = new Body().withText(textBody);
    Message message = new Message().withSubject(subject).withBody(body);

    // Assemble the email.
    SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination).withMessage(message);
    try
    {        
        logger.info("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");

        AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient();

        Region REGION = Region.getRegion(Regions.US_WEST_2);
        client.setRegion(REGION);

        client.sendEmail(request);  
        JSONObject exceptionobj = new JSONObject();
        exceptionobj.put("StatusCode", StatusCode.SUCCESSFUL_CODE);
        exceptionobj.put("Message", "Email Successfully sent!");
        sendingemail.put(exceptionobj);

    }catch (Exception ex){
        JSONObject exceptionobj = new JSONObject();
        exceptionobj.put("StatusCode", StatusCode.EXCEPTION_ERROR_CODE);
        exceptionobj.put("Error Message", ex.getMessage());
        sendingemail.put(exceptionobj);
    }
    return sendingemail;




Aucun commentaire:

Enregistrer un commentaire