Email Messages in Salesforce are powerful, robust and provide secure functionality to send an Email from Salesforce. In this Salesforce tutorial, we will learn about Email Programming, different types of email, What is Single Email Messages in Salesforce and Methods, What is Mass Email Message in Salesforce, Attaching files to outbound emails, Attaching documents to outbound email messages in Salesforce.
Email Messages in Salesforce
Email Messages in Salesforce are powerful, robust and provide secure functionality to send an Email from Salesforce. It is very important to send and receive an Email from Salesforce to the external system then we use Email services. In Salesforce, we have two types of Email Services they are
- Inbound Email Messaging
- Outbound Email Messaging.
Outbound Email Messaging
Outbound Email Messaging is used to send an Email to external system using Apex Code. They are 2 types of outbound Emails.
- Single Email Message.
- Mass Email Message.
Single Email Message
Single Email Message instantiates an email object used for sending a single email message.
Syntax
Messaing.SingleEmailMessage email = new Messaging.SingleEmailMessage();
As shown in above class, Messaging name space provides classes and methods for Salesforce outbound and Inbound email services. The methods in the above class sends single email message to external system. To send single email message, first we have to create an object for SingleEmailmessage.
SingleEmailMessage Methods
Single Email message consists of nearly 19 methods that can be used in Email messages in Salesforce.
SetBccAddresses(bccAddresses)
This method will set Bcc Address to the whom the email should be sent. We can set up to 25 email addresses.
String[] toBccAddresses = new String[] {'tutorialkart@gmail.com', example@gmail.com};
mail.setBccAddress(tobccaddress);
setCcAddresses(ccAddresses)
This method will set CcAddress to whom the mail should be sent. We can set utp 25 email address.
String[] toCcAddresses = new String[] {'tutorialkart@gmail.com', example@gmail.com};
mail.setCcAddress(toccaddress);
setToaddress()
This method will set the address, we can set up to 100 addresses.
String[] toAddress = new String[] {'tutorialkart@gmail.com', example@gmail.com};
mail.setToAddress(toaddress);
setSubject(string)
setSubject( ) method will set the subject of mail.
myemail.setSubject('tutorialkart');
setPlainTextBody()
setPlainTextBody() will set the main body of the mail.
myemail.setPlainTextBody('Welcome to tutorialkart');
setHtmlBody(htmlBody)
setHtmlBody( ) will set the main body of the mail.
myemail.setHtmlBody('<h1> Subscribe to tutoralkart.com'</h1>);
MassEmailMessage
Through MassEmailMessage we can send a mass email messages to a recipient list that consists of contacts, Leads, Person accounts or users you can view in Salesforce.
Messaging.MassEmailMessage
Messaging.MassEmailMessage class all the methods defined in the Email class and also Email base class methods.
Signature : public MassEmailMessage()
- setDescription(description) : This will give the description about the mail.
- setTargetObjectIds(targetObjectIds) : We can add upto 250 IDs per email. If you specify a value for the target Object Id’s field optionally specify a WhatID’s as well.
- setWhatIds(whatIds) : This values can be . any one of the following contact, case, opportunity and product.
Syntax
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();