Mastering Email Automation in Salesforce: A Comprehensive Guide to Email Programming



==>
There may be situations in which you would not want a workflow email alert, because either your  criteria to send an email may be too complex or the person whom you want to send an email has to be determined dynamically.  
==> In such situations you can choose to send an email from a Apex Class.
==> Salesforce allows us to send the email notifications to the users or external systems / to receive the emails from the users.
==> While sending/ Receiving the emails, we can add the Attachments also.

We have 2 Types of Email Services in Salesforce 
    • Inbound Email Messaging
    • Outbound Email Messaging
Note: All Email Classes are available in the "Messaging" name space.
Governor Limits:
In s single transaction, we can have 10 email invocations. 
i.e Maximum of 10 Messaging.SendEmail() methods per Transaction.

Inbound Email:
This service is used to receive the Emails from the users or external systems.
        i.e Email is coming into our Salesforce instance.
It provides a class called as "Messaging.InboundEmailHandler" in-order to receive the email from the user and store the email content in the associated object along with the attachment.

Outbound Emails
Used to send the Emails to the users based on the ToAddress and with the required Subject and the Content
We can able to send the Email Content in PlainText or HTML format
 We have 2 ways to send the Outbound Emails.

1. Messaging.SingleEmailMessage Class
2. Messaging.MassEmailMessage Class

Messaging.SingleEmailMessage Class:

This class is used to send the Email notification to one or more users with the required subject and content at runtime.
It provides a set of methods to be used to supply the values to send send email notification.
We can access those methods, by creating the object of the class.

Syntax:   Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Methods:
  1. SetToAddresses(<To Addresses List>):
  2. SetCCAddresses(<CC Addresses List>).
  3. SetBCCAddresses(<BCC Addresses List>).
  4. SetReplyTo(string emailAddress):.
  5. SetSenderDisplayName(string displayName).
  6. SetSubject(string emailSubject).
  7. SetPlainTextBody(string emailContent).
  8. SetHTMLBody(string htmlEmailContent).
  9. SetFileAttachments(List<Messaging.EmailFileAttachment>).
Messaging.SendEmail():
This method is used to send the Email Notification to the users with the specified subject and the content.

Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email} );
OR
Messaging.SendEmail(List<Messaging.SingleEmailMessage>);

Real-Time Use case for Email Programming
Write an apex Program, to create 10 Lead Records into the object. And notify the Lead Persons through an Email Notification. And add the Email Content as an Attachment along with the Email Notification in the form of a PDF File.


Execution Code:
//Invoke the Method...
MessagingUtility.CreateBulkLeadRecords(2);

Like our post & bookmark this blog for your future learning. 
Any suggestions and improvements are most welcome, please comment your suggestions and improvement in the comment box. 

Happy Coding Sharing Is caring. 😀😀😀

Post a Comment

Post a Comment (0)

Previous Post Next Post