Saturday, November 29, 2014

Outbound Email Services in Salesforce.

Outbound Email Services in Salesforce.

You can use Apex to send individual and mass email. The email can include all standard email attributes (such as subject line and blind carbon copy address), use Salesforce email templates, and be in plain text or HTML format, or those generated by Visualforce.

Visualforce email templates cannot be used for mass email.

To send individual and mass email with Apex, use the following classes:

SingleEmailMessage
 Instantiates an email object used for sending a single email message. The syntax is:
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

MassEmailMessage
Instantiates an email object used for sending a mass email message. The syntax is:
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();



SendEmailResult:
Note:
  • The email is not sent until the Apex transaction is committed.
  • The email address of the user calling the sendEmail method is inserted in the From Address field of the email header. All email that is returned, bounced, or received out-of-office replies goes to the user calling the method.
  • Maximum of 10 sendEmail methods per transaction. Use the Limits methods to verify the number of sendEmail methods in a transaction.
  • Single email messages sent with the sendEmail method count against the sending organization's daily single email limit. When this limit is reached, calls to the sendEmail method using SingleEmailMessage are rejected, and the user receives a SINGLE_EMAIL_LIMIT_EXCEEDED error code. However, single emails sent through the application are allowed.
  • Mass email messages sent with the sendEmail method count against the sending organization's daily mass email limit. When this limit is reached, calls to the sendEmail method using MassEmailMessage are rejected, and the user receives a MASS_MAIL_LIMIT_EXCEEDED error code.
  • Any error returned in the SendEmailResult object indicates that no email was sent.
Sending Single Email From Apex:
public class ContactOutBoundEmailService { //Constructor do nothing. public ContactOutBoundEmailService(){ } public void createContactAndSendEmail(){ Contact contObj = new Contact(); contObj.firstName = 'Srinivasa'; contObj.lastName = 'Balle'; contObj.email = 'sfdcsrini@gmail.com'; contObj.Phone = '919014678568'; contObj.description = 'This is Test contact created for outboundemailservice.'; insert contObj; system.debug('==>created Contact is==>'+contObj.id); // First, reserve email capacity for the current Apex transaction to ensure // that we won't exceed our daily email limits when sending email after // the current transaction is committed. Messaging.reserveSingleEmailCapacity(2); // Processes and actions involved in the Apex transaction occur next, // which conclude with sending a single email. // Now create a new single email message object // that will send out a single email to the addresses in the To, CC & BCC list. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // Strings to hold the email addresses to which you are sending the email. String[] toAddresses = new String[] {contObj.email}; String[] ccAddresses = new String[] {contObj.email}; // Assign the addresses for the To and CC lists to the mail object. mail.setToAddresses(toAddresses); mail.setCcAddresses(ccAddresses); // Specify the address used when the recipients reply to the email. mail.setReplyTo('sfdcsrini@gmail.com'); // Specify the name used as the display name. mail.setSenderDisplayName('Salesforce Support'); // Specify the subject line for your email address. mail.setSubject('New Contact Created : ' + contObj.Id); // Set to True if you want to BCC yourself on the email. mail.setBccSender(false); // Optionally append the salesforce.com email signature to the email. // The email address of the user executing the Apex Code will be used. mail.setUseSignature(true); // Specify the text content of the email. mail.setPlainTextBody('Your Contact: ' + contObj.Id +' has been created.'); mail.setHtmlBody('Your Contact:<b> ' + contObj.Id +' </b>has been created.<p>'+ 'To view your Contact <a href=https://ap1.salesforce.com/'+contObj.Id+'>click here.</a>'); // Send the email you have created. Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }
Mass Email Message:
public class ContactMassOutBoundEmailService { List<Id> contactList = new List<Id>(); List<ID> whatIds = new List<ID>(); //Constructor do nothing. public ContactMassOutBoundEmailService(){ } public void createContactAndSendMassEmail(){ Contact contObj = new Contact(); contObj.firstName = 'Srinivasa'; contObj.lastName = 'Balle'; contObj.email = 'sfdcsrini@gmail.com'; contObj.Phone = '919014678568'; contObj.description = 'This is Test contact created for outboundemailservice.'; insert contObj; contactList.add(contObj.id); system.debug('==>created Contact is==>'+contObj.id); if(contactList !=null && contactList.size()>0){ Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage(); mail.setTargetObjectIds(contactList); //Get the email template id to send the html email message. mail.setTemplateId([SELECT id FROM EmailTemplate WHERE Name = 'CreateContactAndSendMassEmail'].id); mail.saveAsActivity = false; Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail }); } } }

1 comments:

Post a Comment

 
| ,