0% found this document useful (0 votes)
45 views

Email Programming

1. The document describes email programming in Apex and outlines methods for sending outbound emails using SingleEmailMessage and MassEmailMessage classes as well as receiving inbound emails using the InboundEmailHandler interface. 2. It provides details on the properties and methods of the SingleEmailMessage class that can be used to specify recipient addresses, subject, body, attachments and send emails. 3. An example Apex class is given that creates lead records, then sends email notifications with a PDF attachment to the leads using the SingleEmailMessage class.

Uploaded by

sai creatives
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Email Programming

1. The document describes email programming in Apex and outlines methods for sending outbound emails using SingleEmailMessage and MassEmailMessage classes as well as receiving inbound emails using the InboundEmailHandler interface. 2. It provides details on the properties and methods of the SingleEmailMessage class that can be used to specify recipient addresses, subject, body, attachments and send emails. 3. An example Apex class is given that creates lead records, then sends email notifications with a PDF attachment to the leads using the SingleEmailMessage class.

Uploaded by

sai creatives
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Email Programming:

==================

"Messaging" Namespace.
|
|
--> 1. Outbound Email Services:
By using Outbound Email Services, we can send the
Email Notifications to the required people with the required subject, content, CC
addresses, BCC addresses, attachments based on the email id.

We have the below 2 readymade classes to send the


email notifications through programming.

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

2. Inbound Email Services:

By using this feature, we can receive the email


notification from the external email id's sent by the customers / end users.

We can receive the email notifications from the


external systems / email id's by using an interface called as

"Messaging.InboundEmailHandler" interface.
|
--> HandleInboundEmail()
method.

Messaging.SingleEmailMessage Class:
===================================
By using this Class we can send the email notifications to either one or more
people with the required email details along with the attachments also.

"SingleEmailMessage" class provides a set of readymade methods to be used to


specify the details of the email body and subject and To Address and other details.

All the methods are "Instance Methods". Which can be accessible by creating the
object of the class.

Syntax:
Messaging.SingleEmailMessage <objectName> = new
Messaging.SingleEmailMessage();

Ex:
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

Methods:
--------
1. SetToAddresses(List<ToAddresses>):
Ex:
string[] toAddress = new
string[]
{'ram@gmail.com','hr@test.com','ferozjani@gmail.com'};

email.SetToAddresses(toAddress);
2. SetCCAddresses(List<CCAddresses>):
Ex:
string[] ccAddress = new
string[]
{'ram@gmail.com','hr@test.com','ferozjani@gmail.com'};

email.SetCCAddresses(ccAddress);

3. SetBCCAddresses(List<BCCAddresses>):
Ex:
string[] bccAddress = new
string[]
{'ram@gmail.com','hr@test.com','ferozjani@gmail.com'};

email.SetBCCAddresses(bccAddress);

4. SetSenderDisplayName(string):

Ex:
email.SetSenderDisplayName('DELL Inc Customer Support Center');

5. SetReplyTo(string replyToEmailID):

Ex:
email.SetReplyTo('sales@dell.com');

6. SetSubject(<Email Subject>):

7. SetPlainTextBody(<Plain Text Email Content> ):

8. SetHTMLBody(<HTML Email Content> ):

9. SetFileAttachments(List<Messaging.EmailFileAttachment>):

Sending the Email:


-----------------
Messaging.SendEmailResult[] result =
Messaging.SendEmail(List<Messaging.SingleEmailMessage>
emailMessages);

(OR)

Messaging.SendEmailResult[] results =
Messaging.SendEmail(new Messaging.SingleEmailMessage[]
{<emailObjects>});

Governor Limit:
---------------
1. We can add max. of 150 Email Addresses in a single invocation. (i.e.
Including To addresses, CC Addresses and BCC Addresses)

2. Per transaction, we can use max. of 10 Email Invocations.

3. Max. size of Email should not be exceeds 10 MB. (Including Subject,


Content, and Attachments)

UseCase:
========
Create an Apex Class, to Create the Lead Records inside the Lead Object. Once
the Lead Record has been created, then Send the Email Notification to the Lead
Person.
1. And Embed the Email Content inside a "PDF File".
2. Add the PDF File as an attachment along with the Email Notification.

Class Code:
-----------
public class MessagingUtility
{
Public static void CreateNewLeadRecord(integer maxRecords)
{
List<Lead> leadsToInsert = new List<LEad>();

if(maxRecords > 0)
{
for(integer counter = 1; counter <= maxRecords; counter++)
{
Lead ld = new Lead();

ld.FirstName = 'Email ';


ld.LastName = 'Test Lead - '+ counter;
ld.Email = 'emailtestlead'+counter+'@gmail.com';
ld.Company = 'Cognizant Technology';
ld.Status = 'Open - Not Contacted';
ld.Title = 'Project Manager';
ld.Phone = '9900998877';
ld.Fax = '9966554433';
ld.Website = 'www.gmail.com';
ld.Rating = 'Hot';
ld.Industry = 'Technology';
ld.AnnualRevenue = 4000000;

leadsToInsert.Add(ld);
}

if(! leadsToInsert.isEmpty())
{
insert leadsToInsert;

// Invoke the method to send the Email Alerts..


SendEmailAlertsToLeads(leadsToInsert);
}
}
}

Public static void SendEmailAlertsToLeads(List<Lead> lstLeads)


{
// Create a List Collection, to Add Multiple Lead People's Email
Notifications.
List<Messaging.SingleEmailMessage> lstEmails = new
List<Messaging.SingleEmailMessage>();

if(! lstLeads.isEmpty())
{
for(Lead ld : lstLeads)
{
// Prepare the Email Notification..
Messaging.SingleEmailMessage email = new
Messaging.SingleEmailMessage();

string[] toAddress = new string[]{ld.Email,


'ferozjani@gmail.com'};
email.setToAddresses(toAddress);

string[] ccAddress = new String[]{ld.Email, 'hr@gmail.com'};


// email.setCcAddresses(ccAddress);

string[] bccAddress = new string[]{ld.Email, 'hr@gmail.com'};


// email.setBccAddresses(bccAddress);

email.setSenderDisplayName('DELL Sales Team');

email.setReplyTo('sales@dell.com');

email.setSubject('Congratulations '+ ld.FirstName + ' '+


ld.LastName + '..!! Your Lead Record has been created successfully.');

string emailContent = 'Dear '+ld.FirstName + ' '+ ld.LastName+


',<br/><br/>'+
'Thanks for showing interest
in our organization products. <br/><br/>'+
'We are pleased to inform
you, that your Lead Record has been created successfully. <br/><br/>'+
'Here are your Lead
Details... <br/><br/>'+
'Your Lead Record ID ....: '+
ld.Id+
'<br/> Lead Name .....: '+
ld.FirstName + ' '+ ld.LastName+
'<br/> Company Name is.....:
'+ld.Company+
'<br/> Lead Status is....: '+
ld.Status+
'<br/> Lead Email Id.....: '+
ld.Email+
'<br/> Lead Contact
Number....: '+ ld.Phone+
'<br/> Fax Number.....: '+
ld.Fax+
'<br/> Annual Revenue ....:
'+ ld.AnnualRevenue+
'<br/> Industry Name is....:
'+ ld.Industry+
'<br/><br/> Please contact on
the below address, if any queries. One of our Sales Person will contact you
shortly. <br/><br/>'+
'*** This is a Auto-Generated
Email. Please Do Not Reply. <br/><br/> Thanks & Regards, <br/> Dell Sales Team.';
email.setHtmlBody(emailContent);

// Prepare a List Collection, to add Attachments.


List<Messaging.EmailFileAttachment> lstAttachments =
new List<Messaging.EmailFileAttachment>();

// Prepare an attachment..
Messaging.EmailFileAttachment attach = new
Messaging.EmailFileAttachment();
attach.setContentType('Application/pdf');
attach.setFileName(ld.FirstName+ ' '+
ld.LastName+'.pdf');
attach.setBody(Blob.toPdf(emailContent));
lstAttachments.Add(attach);

// Add the Attachments to the Email..


email.setFileAttachments(lstAttachments);

// Add the Email Notification to collection.


lstEmails.Add(email);
}

if(! lstEmails.isEmpty())
{
Messaging.SendEmailResult[] results =
Messaging.sendEmail(lstEmails);
}
}
}
}

Execution:
----------
// Invoke the Method..
MessagingUtility.CreateNewLeadRecord(2);

You might also like