通过带附件的Amazon SES从Salesforce发送电子邮件

时间:2018-08-11 16:50:32

标签: email salesforce amazon apex

尝试使用此代码时出现此错误。

<Message>Keys may not contain ;</Message>

下面的类使用Amazon SES API发送电子邮件。我已经被困了好几个星期了,我也不知道该如何进行。

public static String key = 'KEYHERE';
public static String secret = 'SECRETKEYHERE';
public static String endpoint = 'https://email.us-east-1.amazonaws.com';
@future(callout=true)
public static void sendEmailAsync(String mJson) {
    Email m = (Email)Json.deserialize(mJson, Email.class);
    String boundary = 'gc0p4Jq0M2Yt08jU534c0p';
    String boundary2 = 'Yt08jU534c0pgc0p4Jq0M2';

    HttpRequest httpReq = new HttpRequest();
    httpReq.setMethod('POST');
    httpReq.setEndpoint(endpoint);

    httpReq.setHeader('Host','email.us-east-1.amazonaws.com');
    httpReq.setHeader('Content-Type','application/x-www-form-urlencoded');
    String awsFormattedNow = awsFormattedDate(Datetime.now());
    httpReq.setHeader('Date',awsFormattedNow);
    httpReq.setHeader('X-Amzn-Authorization', headerForAmazonAuthorization(key,signature(awsFormattedNow,secret)));

    String msg = '';
    // header
    msg += 'From:' + m.fromAddress + '\n';
    msg += 'Subject:'+ m.subject + '\n';
    msg += 'MIME-Version:1.0\n';
    msg += 'Content-type:multipart/mixed; boundary="' + boundary + '"\n\n';

    // html and text
    msg += '--' + boundary + '\n';

    msg += 'Content-type:multipart/alternative; boundary="' + boundary2 + '"\n\n';

    // text
    msg += '--' + boundary2 + '\n';
    msg += 'Content-type: text/plain;charset=utf-8\n';
    msg += 'Content-Transfer-Encoding: quoted-printable\n\n';
    msg += m.bodyText;
    msg += '\n\n';

    // html
    msg += '--' + boundary2 + '\n';
    msg += 'Content-Type: text/html; charset=utf-8\n';
    //msg += 'Content-Transfer-Encoding: 7bit\n\n';
    msg += 'Content-Transfer-Encoding: quoted-printable\n\n';
    msg += m.bodyHtml;
    msg += '\n\n';

    // attachment
    if(m.attachment != null) {
        msg += '--' + boundary + '\n';
        msg += 'Content-Transfer-Encoding: base64\n';
        msg += 'Content-Type: '+ m.attachment.contentType +'; name=' + m.attachment.name + ';\n';
        msg += 'Content-Disposition: attachment; filename=' + m.attachment.name + ';\n\n';
        msg += EncodingUtil.base64Encode(m.attachment.body);

    }

    // close
    msg += '\n\n--' + boundary + '--\n';

    System.debug('##msg: ' + msg);
    String encodedMessage = EncodingUtil.base64Encode(Blob.valueOf(msg));
    String requestBody = 'Action=SendRawEmail&amp;Destinations.member.1=' + EncodingUtil.urlEncode(m.toAddress,'UTF-8') + '&amp;RawMessage.Data=' + EncodingUtil.urlEncode(encodedMessage, 'UTF-8');
    System.debug('##requestBody:' + requestBody);

    httpReq.setHeader('Content-Length', String.valueOf(requestBody.length()));

    httpReq.setBody(requestBody);
    Http http = new Http();
    HttpResponse response = http.send(httpReq);

    System.debug('##response: ' + response);
    System.debug('##response body: ' + response.getBody());

}
public class MailAttachment {
    public String name;
    public Blob body;
    public String contentType;
}

public class Email {
    public String displayName;
    public String fromAddress;
    public String toAddress;
    public String subject;
    public String bodyText;
    public String bodyHtml;
    public MailAttachment attachment;
}
private static string awsFormattedDate(Datetime now)
{
    return now.formatGmt('EEE, d MMM yyyy HH:mm:ss Z');
}

private static string headerForAmazonAuthorization(String accessKey, String signature)
{
    return 'AWS3-HTTPS AWSAccessKeyId='+accessKey+', Algorithm=HmacSHA256, Signature='+signature;
}

private static string signature(String awsNow, String s) {
    system.assert( secret != null ,' missing S3.secret key');
    Blob bsig = Crypto.generateMac('HmacSHA256', Blob.valueOf(awsNow), Blob.valueOf(s));
    return EncodingUtil.base64Encode(bsig);
}

我尝试删除代码的附件部分,但它仍然给我同样的错误。

这是我上面的函数的调用方式:

Attachment attach = [SELECT name, body, contentType FROM Attachment LIMIT 1];
AWS_SES.Email newEmail = new AWS_SES.Email();
if (attach.body != null) {
    AWS_SES.MailAttachment newEmailAttachment = new AWS_SES.MailAttachment();
    newEmailAttachment.name = attach.name;
    newEmailAttachment.body = attach.body;
    newEmailAttachment.ContentType = attach.ContentType;
    newEmail.attachment = newEmailAttachment;
}
newEmail.displayName = 'Test Sender';
newEmail.fromAddress = 'testemail@gmail.com';
newEmail.toAddress = 'testemail@gmail.com';
newEmail.subject = 'test';
newEmail.bodyText = 'test';
newEmail.bodyHtml = 'TEST';

AWS_SES.sendEmailAsync(Json.serialize(newEmail));

希望有人可以提供帮助!

谢谢。

0 个答案:

没有答案