Salesforce Email Utility - 自定义Email / 调用标准Email模板

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/itsme_web/article/details/82802988

场景描述】:
做房地产放盘模块时,当交易状态表更或者独家代理过期后我们需要邮件通知置业顾问和相关的stakeholder,由于具体需求还未定,索性开发了针对所有邮件类型模板的工具类,以便后续复用。

功能介绍】:
该工具类主要功能有:
1. 邮箱地址校验;
2. 在Apex种发送标准邮件模板邮件;
3. 发送PlainText/HTML类型文件;
4. 发送带附件的邮件;

代码示例】:

/*******************************************************************************
 * 工具类
 *
 * 功能: 可调用工具
 * 作者: Wilson
 * 日期: 2018/09/21
 *
 ******************************************************************************/
public class EmailUtility {

    public static Boolean isValidEmailAddress(String emailAddress){
        if(emailAddress != null && emailAddress.trim() != null && emailAddress.trim().length() > 0){
            String[] split = emailAddress.split('@');
            if(split != null && split.size() == 2){
                String prefix = split[0];
                split = split[1].split('\\.');
                if(prefix.length() > 0 && split != null && split.size() >= 2){
                    return true;
                }
            }
        }
        return false;
    }   
    
    public static void sendEmailWithStandardAttachments(List<String> recipients,String emailSubject,String body,Boolean useHTML,List<String> attachmentIDs) {
        List<Attachment> stdAttachments = [SELECT id, name, body FROM Attachment WHERE Id IN:attachmentIDs];
        sendEmailWithStandardAttachments(recipients, emailSubject, body, useHTML, stdAttachments);
    }
    
    public static void sendEmailWithStandardAttachments(List<String> recipients,String emailSubject,String body,Boolean useHTML,List<Attachment> stdAttachments) {
        List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.EmailFileAttachment>();
        
        for(Attachment attachment : stdAttachments) {
            Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
            fileAttachment.setFileName(attachment.Name);
            fileAttachment.setBody(attachment.Body);
            fileAttachments.add(fileAttachment);
        }
        sendMail(recipients, emailSubject, body, useHTML, fileAttachments);
    }
     
    public static void sendTextEmail(List<String> recipients,String emailSubject,String textBody) { 
        sendMail(recipients, emailSubject, textBody, false, null);
    }
    
    public static void sendHTMLEmail(List<String> recipients,String emailSubject,String htmlBody) { 
        sendMail(recipients, emailSubject, htmlBody, true, null);
    }
    
    private static void sendMail(List<String> recipients, String emailSubject, String body, Boolean useHTML, List<Messaging.EmailFileAttachment> fileAttachments) {
        
        if(recipients == null) return;
        if(recipients.size() == 0) return;
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
        // save email as activity on the targetObjId (i.e. Contact). Note activity can't be saved on Users   
        mail.setSaveAsActivity(false);
        // The maximum number of email addresses allowed is 100.
        mail.setToAddresses(recipients);
        mail.setSubject(emailSubject);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        // set EmailBody
        if (useHTML) {
            mail.setHtmlBody(body);
        } else {
            mail.setPlainTextBody(body);
        }
        // Specify FileAttachments
        if(fileAttachments != null && fileAttachments.size() > 0) {
            mail.setFileAttachments(fileAttachments);
        }
        // Sends mail here
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });   
        
    }   

    public static void sendTemplateEmail(String templateId, String targetObjId, String whatId, String orgWideEmailId, String senderName, Boolean logActivity) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  

        mail.setTemplateId(templateId);
        // The ID of the contact, lead, or user to which the email will be sent and ensures that merge fields in the template contain the correct data. 
        mail.setTargetObjectId(targetObjId);
        // If you specify a contact for the targetObjectId field, you can specify an optional whatId as well.
        mail.setWhatId(whatId);

        if(orgWideEmailId != null) {
            mail.setorgWideEmailAddressId(orgWideEmailId); 
        }   
        // save email as activity on the targetObjId (i.e. Contact). Note activity can't be saved on Users  
        mail.setSaveAsActivity(false); // default false
        if(logActivity != null) {
            mail.setSaveAsActivity(logActivity);
        }
        mail.setSenderDisplayName(senderName);

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }


}

相关效果】:
1. 测试使用标准邮件模板的邮件
参数1 - 邮件模板Id, 参数2 - Case记录上的Contact ID(支持Lead / Contact / User ID),参数3 - Case ID,参数4 - 设置后Email的sender email将显示所对应的Email,参数5 - sender name,参数6 - 记录一个Activity;

EmailUtility.sendTemplateEmail('00X6F000002d8SK', '0035D00000Sa6xV', '5005D000003xgsv', null, 'E&V Test Email', true);


2. 测试带附件的文本邮件

List<String> recipients = new List<String>{'[email protected]', '[email protected]'};
String emailSubject = 'Test Email with an attach file';
String body = 'this is a test email.';
Boolean useHTML = false;
List<String> attachmentIDs = new List<String>{'00P5D000005DWYe'};
EmailUtility.sendEmailWithStandardAttachments(recipients, emailSubject, body, useHTML, attachmentIDs);

猜你喜欢

转载自blog.csdn.net/itsme_web/article/details/82802988
今日推荐