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

Email Programming

Uploaded by

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

Email Programming

Uploaded by

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

Email Programming:

==================
Outbound Email Services:
To send the Emails to the people

Salesforce provides the below 2 Classes.

1. System.Messaging.SingleEmailMessage class
2. System.Messaging.MassEmailMessage Class

Inbound Email Services:


Used to receive the email notifications from the external email id's.

Salesforce provides the below interface.

1. System.Messaging.InboundEmailHandler interface

SingleEmailMessage Class:
-------------------------
By using this class, we can send the email notifications to one or more users with
the required subject, content, attachments, CC Addresses and BCC address
dynamically at runtime.

It provides a set of readymade methods to be get used to send the email


notifications. All the instance methods.

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

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

Methods:
1. SetToAddresses(<List Of Addresses>) :
(Max.Of 100 Email Id's)

String[] toAddresses = new


string[]{'[email protected]','[email protected]'};

email.SetToAddresses(toAddresses);

2. SetCCAddresses(<List Of CC Addresses>):
(Max.Of 25 Email Id's)

String[] ccAddresses = new


string[]{'[email protected]','[email protected]'};

email.SetCCAddresses(ccAddresses);

3. SetBCCAddresses(<List Of BCC Addresses>):


(Max.Of 25 Email Id's)

String[] bccAddresses = new


string[]{'[email protected]','[email protected]'};

email.SetBCCAddresses(bccAddresses);
4. SetSubject(<Email Subject>):
Subject of the Email Notification.

email.SetSubject('Congratulations..!! Your Record Created.');

5. SetSenderDisplayName(<Sender Display Name>):

email.SetSenderDisplayName('Reminder : CITI Bank Credit Card


Department.');

6. SetReplyTo(<Reply To Email ID>):

email.SetReplyTo('[email protected]');

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

email.SetPlainTextBody(<Email Content>);

8. SetHTMLBody(<HTML Content>):

email.SetHTMLBody(<HTML Email Body>);

9. SetFileAttachments(List<EmailFileAttachment>):

To Send the Email:


------------------
System.Messaging.SendEmailResult[] <results> =
system.Messaging.SendEmail(List<Messaging.SingleEmailMessage>);

(OR)

System.Messaging.SendEmail(new
System.Messaging.SingleEmailMessage[]{<objectName>});

/*
Write an apex program, To Insert 5 Lead Records into the object. And send the
Email Notification to the Lead People with the Lead Details.

And include the Email Content inside a "PDF File" and attach the PDF file
along with the Email Content as an attachment.
*/
Class Code:
-----------
public class MessagingUtility
{
Public static void InsertBulkLeadRecords(integer maxRecords)
{
if(maxRecords > 0)
{
List<Lead> lstLeads = new List<Lead>();

for(integer counter = 1; counter <= maxRecords; counter++)


{
Lead ld = new Lead();

ld.FirstName = 'Bulk';
ld.LastName = 'Test Lead'+ counter;
ld.Email = 'bulk'+counter+'@gmail.com';
ld.Company = 'Cognizant Inc.,';
ld.Status = 'Open - Not Contacted';
ld.Industry = 'Finance';
ld.Rating = 'Hot';
ld.AnnualRevenue = 5800000;
ld.Phone = '9900998899';
ld.Fax = '9900887766';
ld.Website = 'www.cognizant.com';

lstLeads.Add(ld);
}

if(! lstLeads.isEmpty())
{
Insert lstLeads;

// Call the method to send the email alerts..


SendEmailNotificationsToLeads(lstLeads);
}
}
}

Public static void SendEmailNotificationsToLeads(List<Lead> leadRecords)


{
List<Messaging.SingleEmailMessage> lstEmails = new
List<Messaging.SingleEmailMessage>();

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

string[] toAddresses = new string[]{'[email protected]',


ld.Email};
email.setToAddresses(toAddresses);

string[] ccAddresses = new string[]{'[email protected]',


ld.Email};
//email.setCcAddresses(ccAddresses);

string[] bccAddresses = new string[]{'[email protected]',


ld.Email};
//email.setBccAddresses(bccAddresses);

email.setSenderDisplayName('Reminder : CITI Bank Credit Card


Department.');

email.setReplyTo('[email protected]');

string emailSubject = 'Congratulations '+ ld.FirstName + ' '+


ld.LastName + ' ..!! Your Lead Record has been Created Successfully.';
email.setSubject(emailSubject);

string emailContent = 'Dear '+ ld.FirstName+ ' '+ ld.lastname+ ',


<br/> <br/>'+
'Thanks for showing your interest in our organization
products. <br/><br/>'+
'We are pleased to inform you, that your Lead Record has
been created successfully inside our Database for future reference.<br/><br/>'+
'Here are your Lead Details... <br/><br/>'+
'Lead Name : '+ ld.FirstName + ' '+ ld.LastName+
'<br/> Lead Record ID : '+ ld.Id+
'<br/> Company Name is...: '+ ld.Company+
'<br/> Lead Status .....: '+ ld.Status+
'<br/> Industry Name.....: '+ ld.Industry+
'<br/> Annual Revenue is...: '+ ld.AnnualRevenue+
'<br/> Rating Value is...: '+ ld.Rating+
'<br/> Contact Number is...: '+ ld.Phone +
'<br/> Fax Number is.....: '+ ld.Fax+
'<br/><br/> *** This is an auto-generated email. Please Do
Not Reply.'+
'<br/><br/> Please contact on the below address, if any
queries.'+
'<br/><br/> Thanks & Regards, <br/> Dell Sales Team <br/>
Dell Inc.';
email.setHtmlBody(emailContent);

List<Messaging.EmailFileAttachment> lstAttachments = new


List<Messaging.EmailFileAttachment>();

// Preparing the Attachments...


Messaging.EmailFileAttachment attach = new
Messaging.EmailFileAttachment();

attach.setFileName(ld.FirstName + ' '+ ld.LastName+'.pdf');


attach.body = Blob.toPdf(emailContent);

// Add the attachment to the collection..


lstAttachments.Add(attach);

// Add the Attachments to the Email...


email.setFileAttachments(lstAttachments);

// Add the email alert to the collection..


lstEmails.Add(email);
}

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

Execution:
----------
// Invoking the method..
MessagingUtility.InsertBulkLeadRecords(3);

You might also like