Wednesday, January 18, 2023

Email Programming in Salesforce

When we want to send the email notifications dynamically, we use apex based Email service.

We have 2 types of email service.

  • Outbound Email: If we are sending email from salesforce to external system, then we call it as outbound email services.
  • Inbound Email: If we are sending email from external system to salesforce, then we call it as Inbound email services.

All the classes & methods required to handle the email services are defined in messaging namespace

Outbound message

  • We can able to send the Email context or plain text or HTML format.
  • We can also attach email templates & Attachments to the email.
  • We have 2 ways to send the outbound 1
    • messaging.singleEmailMessage
    • 2.Messaging.MassEmailMessage

messaging.singleEmailMessage

Methods

  1. setToAddresses(List<string> emailAddress): To specify the To Address interms of string Array or List. Note:  The maximum allowed ToAddresses is 100.
  2. setCCAddresses(List<string>emailAddress):This method is used to set one or more CC Email Addresses. Note: The maximum Allowed is 25.
  3. setBCCAddress(List<string>emailAddress):This method is used to set one or more BCC Email Addresses. Note : The maximum Allowed is 25.
  4. setReplyTo(string):
  5. setSenderDisplayName(string):This method is used to set the display name of sender in email .Example : ‘’SalesforceCodes’’.
  6. setSubject(string EmailSubject):
  7. setPlainTextBody(string Email context): [set format in plain]
  8. setHTMLBody(string email HTML context).

Note: If we want to send the email we have to use senderEmail(List<Email>) method defined in messaging class.

Syntaxes

  1. 1.Messaging.Email[] emails = new messaging.Email[]{email1,email2,….};
    1. Messaging.sendEmail(emails);
  1. 2. Messaging.sendEmail(List<stringEmailMessageObject> emails);
    1. This method is defined in the messaging namespace.
    2. This method is used to send the single/multiple email messages.

Outbound Email with PDF Attachments

Messaging.EmailFileAttachment :

This is predefined class which is used to create PDF Attachment  for Emails. Email FileAttachment is used in singleEmailMessage to specify attachment passed is,as part of the Email outbound message.

Methods of EmailFileAttachment :

  1. 1. setFileName(FileName) :
  2. This method is used to set the name of the email Attachment file.
  • 2. setBody(Bold) :
  • This method is used to set the body of the attachment is the format of bold.
  • 3.setContextType(Context Type):
  • This will specify the context type of the email Attachment.

Email Template As Body

Email templates are attachment as body of the email only when we want to send email to

  1. Lead.
  2. Contact.
  3. User.

setTargetObjectId(ID of the user/contact): When we want to send the email template particular user/contact/Lead .

SetTargetObjectIds(List <id> contacts/users/Leads): This method  can be used is MassEmailMassage only.

SetWhatId(Id): This method is optional. Used only if forget object Id is Contact id. In the Email Template if you are using data from any record ,the that record id should be pressed as WhatId.

Messaging.MassEmailMessage

  • This class is used to send the Bulk Email to the salesforce users (contacts/users/Leads).
  • We can send email up to 250 users in a single instance.
  • We have to add all the user ID’s is the list n order to send the Email Notification.

 Note : While sending the email , we can use Email Templates also

Methods

  1. setTargetObjectIDs(List<ID>)
  2. setEmailTemplateID(EmailTemplate ID)

Inbound Email Services :

Syntax

Messaging.InboundEmailResult handleInboundEmail(messaging.InboundEmail email,messaging.InboundEnvelope envelope){ }

messaging.InboundEmailresult : This class specifies weather inbound email is handled successfully or not.

Messaging.InboundEmail : This Class Contains all data received from Inbound Email.

Messaging.InboundEnvelope

No comments:

Post a Comment