1. 程式人生 > >Salesforce Email Utility

Salesforce Email Utility

場景描述】: 做房地產放盤模組時,當交易狀態表更或者獨家代理過期後我們需要郵件通知置業顧問和相關的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<Id> 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

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