Amazon SES发送带附件的电子邮件

时间:2014-09-26 09:39:35

标签: amazon-web-services salesforce

我正在尝试使用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','sample@gmail.com');
    httpReq.setHeader('To','sample@gmail.com');
    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);

我收到错误

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

请在我做错的地方帮助我。谢谢提前

1 个答案:

答案 0 :(得分:1)

再看看文档。您的代码存在一些问题。

SES期望HTTP POST与所有参数串在一起,与application/x-www-form-urlencoded POST请求一致。

您的HTTP请求必须是Content-type: application/x-www-form-urlencoded,而不是multipart/mixed... - 您尝试发送的原始邮件的一部分。

您正在混合应该在正文中的内容,并设置HTTP请求标头。例如,这些也是不正确的:

httpReq.setHeader('From','sample@gmail.com');
httpReq.setHeader('To','sample@gmail.com');
httpReq.setHeader('Subject','Hello');

这些应该放在请求正文中,而不是放在HTTP请求标头中。此外,值是urlencoded。从示例代码:

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

为了清晰起见,添加了换行符。

尝试成功发送简单的电子邮件,然后尝试修改代码以支持附件,可能最有利于您的兴趣,因为在此代码生效之前,您需要纠正许多错误正常。

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