Email Programming
Email Programming
==================
Outbound Email Services:
To send the Emails to the people
1. System.Messaging.SingleEmailMessage class
2. System.Messaging.MassEmailMessage Class
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.
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)
email.SetToAddresses(toAddresses);
2. SetCCAddresses(<List Of CC Addresses>):
(Max.Of 25 Email Id's)
email.SetCCAddresses(ccAddresses);
email.SetBCCAddresses(bccAddresses);
4. SetSubject(<Email Subject>):
Subject of the Email Notification.
email.SetReplyTo('[email protected]');
email.SetPlainTextBody(<Email Content>);
8. SetHTMLBody(<HTML Content>):
9. SetFileAttachments(List<EmailFileAttachment>):
(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>();
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;
if(! leadRecords.isEmpty())
{
for(Lead ld : leadRecords)
{
// Prepare the email content..
Messaging.SingleEmailMessage email = new
Messaging.SingleEmailMessage();
email.setReplyTo('[email protected]');
if(! lstEmails.isEmpty())
{
Messaging.SendEmailResult[] results =
Messaging.sendEmail(lstEmails, false);
}
}
}
}
Execution:
----------
// Invoking the method..
MessagingUtility.InsertBulkLeadRecords(3);