在Salesforce中处理Email的发送

在Salesforce中可以用自带的 Messaging 的 sendEmail 方法去处理Email的发送

请看如下一段简单代码:

public boolean TextFormat {get;set;}
public string EmailTo {get;set;}
public string EmailCC {get;set;}
public string EmailBCC {get;set;}
public string EmailSubject {get;set;}
public string EmailBody {get;set;}

public string MoreAttachName1 {get;set;}
public blob MoreAttachBody1 {get;set;}
public string MoreAttachName2 {get;set;}
public blob MoreAttachBody2 {get;set;}
public string MoreAttachName3 {get;set;}
public blob MoreAttachBody3 {get;set;}

public PageReference Send(){

     List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
     // Add attachments
     if(MoreAttachBody1 != null) attachments.add(CreateEmailAttachment(MoreAttachName1, MoreAttachBody1));
     if(MoreAttachBody2 != null) attachments.add(CreateEmailAttachment(MoreAttachName2, MoreAttachBody2));
     if(MoreAttachBody3 != null) attachments.add(CreateEmailAttachment(MoreAttachName3, MoreAttachBody3));
    
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     mail.setUseSignature(false);
     mail.setToAddresses(EmailTo.split(';',0));
     if(EmailCC != '') mail.setCcAddresses(EmailCC.split(';',0));
     if(EmailBCC != '') mail.setBccAddresses(EmailBCC.split(';',0));
     mail.setSubject(EmailSubject);
     if(TextFormat) mail.setPlainTextBody(EmailBody);
     else mail.setHtmlBody(EmailBody);
     if(attachments.size() > 0) mail.setFileAttachments(attachments); 
 
     // Send the email
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    
     PageReference page = new PageReference('/'+Id);
     page.setRedirect(true);
     return page;
}

private Messaging.EmailFileAttachment CreateEmailAttachment(string name, blob body) {
     Messaging.EmailFileAttachment emailAttach = new Messaging.EmailFileAttachment();
     emailAttach.setFileName(name);
     emailAttach.setInline(false);
     emailAttach.Body = body;
     return emailAttach;
}

 

如果想了解更多的细节,请看如下链接:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_Messaging_instance_methods.htm 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_Messaging_SingleEmailMessage_instance_methods.htm

 

 

 

posted @ 2013-12-05 11:02  Eric Sun  阅读(1543)  评论(0编辑  收藏  举报