Sunny
Sunny

Reputation: 103

Amazon SES Sending email with attachments

I am trying to send mails with attachment by using Amazon SES

    HttpRequest httpReq = new HttpRequest();
    httpReq.setMethod('POST');
    httpReq.setEndpoint('https://email.us-east-1.amazonaws.com');
    Blob bsig = Crypto.generateMac('HmacSHA256', Blob.valueOf(awsFormattedNow), Blob.valueOf(secret));
    httpReq.setHeader('X-Amzn-Authorization','AWS3-HTTPS AWSAccessKeyId='+key+', Algorithm=HmacSHA256, Signature='+EncodingUtil.base64Encode(bsig));
    httpReq.setHeader('Date',awsFormattedNow);

    httpReq.setHeader('From','[email protected]');
    httpReq.setHeader('To','[email protected]');
    httpReq.setHeader('Subject','Hello');

    httpReq.setHeader('Accept-Language','en-US');
    httpReq.setHeader('Content-Language','en-US');
    httpReq.setHeader('Content-Type','multipart/mixed;boundary=\"_003_97DCB304C5294779BEBCFC8357FCC4D2\"');
    httpReq.setHeader('MIME-Version','1.0');

//    httpReq.setHeader('Action','SendRawEmail');


    String email = 'Action=SendRawEmail';

    email += '--_003_97DCB304C5294779BEBCFC8357FCC4D2 \n Content-Type: text/plain; charset="us-ascii" \n Content-Transfer-Encoding: quoted-printable \n';

    email +='Hi Andrew.  Here are the customer service names and telephone numbers I promised you.';

    httpReq.setBody(email);
    System.debug(httpReq.getBody());
    Http http = new Http();
    HttpResponse response = http.send(httpReq);

I am getting error like

<AccessDeniedException>
  <Message>Unable to determine service/operation name to be authorized</Message>
</AccessDeniedException>

Kindly please help me where i am doing wrong .Thanks in advance

Upvotes: 0

Views: 1245

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 178974

Take another look at the documentation. There are several issues with your code.

SES expects an HTTP POST with all of the parameters strung together consistent with application/x-www-form-urlencoded POST requests.

Your HTTP request needs to be Content-type: application/x-www-form-urlencoded, not multipart/mixed... -- that's part of the raw message you're trying to send.

You are mixing up things that should be in the body, and setting HTTP request headers, instead. For example, these are also incorrect:

httpReq.setHeader('From','[email protected]');
httpReq.setHeader('To','[email protected]');
httpReq.setHeader('Subject','Hello');

These should go in the request body, not in the HTTP request headers. Also, the values are urlencoded. From the example code:

Action=SendEmail
&Source=user%40example.com
&Destination.ToAddresses.member.1=allan%40example.com

The line breaks were added for clarity.

Your interests might be best served by trying to successfully send a simple e-mail, first, and then later attempting to modify your code to support attachments, because you have numerous errors that will need to be corrected before this code will work properly.

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/query-interface-requests.html http://docs.aws.amazon.com/ses/latest/APIReference/API_SendRawEmail.html

Upvotes: 1

Related Questions