Concepts or coding lessons of Salesforce that you can implement easily

Understand How To Send Emails From Your Apex Class In Salesforce Before You Start

Sending an email from Apex Class is so easy.


The Apex Messaging.SingleEmailMessage class handles the email functionality available to Salesforce.
Basically there are two categories
  • SingleEmailMessage - This class is used when a use case requirement to send an email to a single person. This contains methods for sending single email messages.
  • MassEmailMessage - This class is used when a use case requirement to send an email to a group

Here I am explaining SingleEmailMessage category. Below is the code snippet which sends an email to single user from apex. I have created one visual force page and controller related to it. Visual force page have Send Email button. 
Once user clicks on the button, I am sending email using apex code. 
Try this for sending emails to a single user.


Step 1:

Create Apex Class with name DemoSendEmail. Paste below code snippet.

public class DemoSendEmail
{
     public void SendEmail()
     {
        String subject = 'Testing email demo';
        String body = 'This is testing for Send emails from your apex class in Salesforce';       
                                   
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        // Now sets the below paramaters of the email object
        email.setSubject( subject );
        // Here I am accessing current user email id to whom we are sending email
        email.setToAddresses( new String[] {UserInfo.getUserEmail()} );
        email.setPlainTextBody( body );
        // Sends the email
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
      }
}

Step 2:

Create a VF page named as "DemoSendEmail" and paste the code below.

<apex:page Controller="DemoSendEmail">
    <apex:form>
         <apex:commandButton value="Send Email!" action="{!SendEmail}"/>
     </apex:form>
</apex:page>

Step 3:

You can click on Preview button which is on Visualforce page 
OR 
You can Go to the URL and type (Change the yoursalesforceinstance with your salesforce org URL)


Click on Send Email button











Done !!!


Explanation about 
SingleEmailMessage:


The following are methods for SingleEmailMessage.

  • setBccAddresses(bccAddresses) : This is Optional. This contains list of blind carbon copy (BCC) addresses. If the BCC compliance option is set in your org at the organization level, then the user cannot add any BCC addresses on standard messages. You will get the below error code: BCC_NOT_ALLOWED_IF_BCC_ COMPLIANCE_ENABLED. You can contact to your Salesforce representative for information on BCC compliance.
  • setCcAddresses(ccAddresses): This is Optional. This contains list of carbon copy (CC) addresses
  • setCharset(characterSet) : This is Optional . The character set for the email. If character set value is null, then the user's default value is used.
  • setDocumentAttachments(documentIds) : This is Optional and Deprecated. Instead of this use setEntityAttachments() instead.
  • setEntityAttachments(ids) : This is Optional.
  • setFileAttachments(fileNames): This is Optional.
  • setHtmlBody(htmlBody) : This is Optional.
  • setInReplyTo(parentMessageIds) : This is Optional.
  • setOptOutPolicy(emailOptOutPolicy) : This is Optional.
  • setPlainTextBody(plainTextBody) : This is Optional.
  • setOrgWideEmailAddressId(emailAddressId) : This is Optional.
  • setReferences(references) : This is Optional.
  • setSubject(subject) : This is Optional.
  • setTargetObjectId(targetObjectId): This is required if using a template, if not then its optional.
  • setToAddresses(toAddresses) : This is Optional. The maximum number of email addresses allowed in setToAddresses method is 100.
  • setTreatBodiesAsTemplate(treatAsTemplate) : This is Optional. If this set to true, then the plain text, subject, and HTML text bodies of the email message are treated as template data.
  • setTreatTargetObjectAsRecipient(treatAsRecipient) : This is Optional.
  • setWhatId(whatId)
For more SingleEmailMessage details check Salesforce documentation related to  : SingleEmailMessage

Enjoy! If you have any questions, comments, please feel free to let me know.
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.



loading...

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey, keep sharing such informative posts here. I also have been finding right ways to connect to the audience in the easy manner. Well, someone suggested using the platform to send sms to slack so that the customers can contact us as quickly as possible without any issues. I was just wondering if you could review such platforms!

    ReplyDelete