2014-09-26 256 views
0

我試圖用附件發送郵件通過使用Amazon SES亞馬遜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); 

我收到錯誤,如

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

請請幫我在哪裏,我做錯了發送電子郵件。在此先感謝

回答

1

再看看文檔。你的代碼有幾個問題。

SES預計所有參數串在一起的HTTP POST與application/x-www-form-urlencoded POST請求一致。

您的HTTP請求需要爲Content-type: application/x-www-form-urlencoded,而不是multipart/mixed... - 這是您嘗試發送的原始消息的一部分。

您正在混合應該在正文中的內容,並設置HTTP請求標頭。例如,這些也是不正確的:

httpReq.setHeader('From','[email protected]'); 
httpReq.setHeader('To','[email protected]'); 
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

+0

嗨@邁克爾,我意識到看到這個帖子後的錯誤,我無法理解其進入頭部,哪些不是,以及如何設置邊界(頭或身體)。你能不能給我一些示例代碼來發送沒有附件的原始消息,這樣我會更清楚。感謝您的回覆 – Sunny 2014-09-27 07:49:35

+0

嗨@邁克爾,我做到了。非常感謝您寶貴的建議,幫助了我很多 – Sunny 2014-09-28 12:11:46