Sunday 8 November 2015

SingleEmailMessage Vs MassEmailMessage | Send Email by Apex


SingleEmailMessage Vs MassEmailMessage





SINGLE EMAILMASS EMAIL
Multiple recipients?YesYes
Personalized body?Yes (single body only)Yes
Special permission needed?NoYes, has to be enabled
Merge fields?YesYes
Personalized merge fields?Yes (only one record at a time)Yes
Templates?YesYes
Template possibilities?Text/HTML/Visualforce/Custom TemplatesText/HTML/Custom Template


SingleEmailMessage:

Single emails are like regular individual emails that may go to one or more addresses (to/cc/bcc), but each of these emails has the same body

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'abc@gmail.com', 'xyz@gmail.com' };
message.optOutPolicy = 'FILTER';
message.subject = 'Opt Out Test Message';
message.plainTextBody = 'This is the message body.';
Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

if (results[0].success) 
{
    System.debug('The email was sent successfully.');
} else 
{
    System.debug('The email failed to send: ' + results[0].errors[0].message);
}
Please check below post to see all other method
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm 

 
MassEmailMessage:

Mass emails typically go to a large number of addresses (currently capped to 250 per email), with personalized message bodies.
public void SendEmail()
{
 List<contact> lstcon=[Select id from contact limit 2];
 List<Id> lstids= new List<Id>();
 for(Contact c:lstcon)
 {
  lstids.add(c.id);
 }
 EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];
 
 Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
 mail.setTargetObjectIds(lstIds);
 mail.setSenderDisplayName('System Admin');
 mail.setTemplateId(et.id);
 Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
 Please check below post to see all other method

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_mass.htm 

 
Related link
https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com 
 
Please let us know if this will help you

Thanks
Amit Chaudhary

 

3 comments:

  1. The Salesforce CRM system is a business tool that allows to manage the customers, partners and prospect information in one place. The companies nowadays use salesforce CRM tool to close bigger deals faster.
    salesforce training in chennai | salesforce training institute in chennai

    ReplyDelete
  2. These kinds of list will going to be a big help for all email marketing programs. With the help of this list everything will be so easy.If you want to know about email list marketing services then check it here at.
    Salesforce CRM Users Email List

    ReplyDelete
  3. HI amit can you please tell me how to find specific unsuccessful/failed email from the email list that we are sending

    ReplyDelete