Email Programming
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.
1. Messaging.SingleEmailMessage Class
2. Messaging.MassEmailMessage Class
"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.
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>):
9. SetFileAttachments(List<Messaging.EmailFileAttachment>):
(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)
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();
leadsToInsert.Add(ld);
}
if(! leadsToInsert.isEmpty())
{
insert leadsToInsert;
if(! lstLeads.isEmpty())
{
for(Lead ld : lstLeads)
{
// Prepare the Email Notification..
Messaging.SingleEmailMessage email = new
Messaging.SingleEmailMessage();
email.setReplyTo('sales@dell.com');
// 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);
if(! lstEmails.isEmpty())
{
Messaging.SendEmailResult[] results =
Messaging.sendEmail(lstEmails);
}
}
}
}
Execution:
----------
// Invoke the Method..
MessagingUtility.CreateNewLeadRecord(2);