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 });
}
Is generally faster for the user (you go through a wizard once instead of once per contact.)
Cons
Cannot customize the email template on a per-contact basis
Might not be able to create a filter to select the recipients
Visualforce email templates cannot be used
Individual Emailing
Pros
Can customize the email template before sending if you select a text template
Does not require Extended Mail Merge to be activated
Does not require a filter to select recipient
Cons
Time consuming for a large number of emails
Single email and Mass email options are avaialble both on UI and the Platform options in Salesforce.
Single email in UI can be used by workflows, alerts and notifications etc, where as mass emails can be used in the UI to send mass emails to contacts and leads using a template.
Through APEX we can use the same functionality with additional programming and logic.
1) http://amitsalesforce.blogspot.in/2015/11/singleemailmessage-vs-massemailmessage.html
2) http://amitsalesforce.blogspot.in/2015/11/default-from-address-while-sending.html
Please let us know if this will help you
Thanks,
Amit Chaudhary
Mass Emailing
Pros
- Is generally faster for the user (you go through a wizard once instead of once per contact.)
Cons- Cannot customize the email template on a per-contact basis
- Might not be able to create a filter to select the recipients
- Visualforce email templates cannot be used
Individual EmailingPros
- Can customize the email template before sending if you select a text template
- Does not require Extended Mail Merge to be activated
- Does not require a filter to select recipient
ConsSingle email and Mass email options are avaialble both on UI and the Platform options in Salesforce.
Single email in UI can be used by workflows, alerts and notifications etc, where as mass emails can be used in the UI to send mass emails to contacts and leads using a template.
Through APEX we can use the same functionality with additional programming and logic.
See the links below,
Email Overview:
https://help.salesforce.com/apex/HTViewHelpDoc?id=email.htm&language=en
Single email:
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_sendemail.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_forcecom_email_outbound.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm
Mass email:
https://help.salesforce.com/HTViewHelpDoc?id=email_mass.htm&language=en_US
https://help.salesforce.com/apex/HTViewHelpDoc?id=email_mass_notes.htm&language=en
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_mass.htm
Apex:
http://wiki.developerforce.com/page/An_Introduction_To_Email_Services_on_Force.com
Few more useful URLs:
https://www.sundoginteractive.com/blog/five-points-to-remember-when-sending-emails-with-salesforce
http://sfdcsrini.blogspot.com/2014/11/outbound-email-services-in-salesforce.html
http://salesforceondemand.blog.com/2012/01/27/to-send-email-from-apex-using-email-template/
http://www.soliantconsulting.com/blog/2009/07/how-to-avoid-governor-limits-with-sendemail-in-apex
Please do let me know if it helps you.
Regards,
Mahesh