0

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

1 Answer 1

1

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

2
  • Hi @michael , i realized the error after seeing this post, i am unable to understand which goes into headers and which are not and how to set boundary(headers or body). can you please give me some sample code to send raw message without attachments, so that i will be more clear. Thanks for your response
    – Sunny
    Commented Sep 27, 2014 at 7:49
  • Hi @Michael, i am done it. Thanks a lot for your valuable suggessions,helped me alot
    – Sunny
    Commented Sep 28, 2014 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.