Email Programing in Outbound Email services

Email Programing :
------------------------------
When we want t send the email notification dynamically we will use apex based Email service.

All the classes and methds that are used to send email or to handle Inbound emails are defined in Messaging Name space

We have  two types of Email service in salesforce.

Outbound Email Services
Inbound Email Services

Outbound Email services:
---------------------------------------
These are used to send the Emails to the users who are outside your organization.

We can able to send the email Content as Plain Text or Html  format.

We can also attach email templates and Attachments to the email

We have 2 ways to send the Outbound Emails.
i) Messaging.SingleEmailMessage
ii)Messaging.MassEmailMessage

Messaging.SingleEmailMessage:
------------------------------------------------
This class is used the OutboundEmails to specified user/users based on business requirement.

Methods:
--------------
setToAddresses(List<String> emailAddress):
To specify the to address in terms of String Array or List
Note :The Maximum Allowed to addreses is 100.

setCcAdresses(List<string> emailAddress):
This method is used to one or more CC Email Addresses.
The Maximum Allowed addresses is 25.

setBccAddresses(List<String> emailAddress):
This method is used to one or more BCC Email Addresses.
The maximum allowed addresses is 25.

setReplyTo(string);
To specify the Reply-To Email Address.

setSenderDisplayName(string);
This method is used to set the Display Name of the Sender in email.
Ex:"Satish satti"

setSubject(string EmailSubject):
This method is used to set the Subject of the email

setPlainTextBody(String emailContent):
this method is used to set the body of the email in the format of palin text format.
setHtmlBody(string emailHtmlContent):
This method is used to set the body of email as html content.

Note:if we want to send the email we have to use sendEmail(List<Email>) method defined in Messaging class.

Messaging.sendEmail(List<SingleEmailMessageObject> emails):

This method is defined in the Messaging namespace.
This method is used to send the SingleEmailMessage | MassEamilMessages

Example:

Create Appex class to send outbound email message with plain text and html body.
----------------------------------------------------------------------------------------------------

public class OutboundExmple {
public void myEmails(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toadd = new String[]{'vadlathalaragava@gmail.com'};
String[] toccadd = new String[]{'vrvreddy0007@gmail.com'};
String[] tobccadd = new String[]{'aneroid.ragava@gmail.com'};

email.setToAddresses(toadd);
email.setBccAddresses(tobccadd);
email.setCcAddresses(toccadd);
email.setSubject('This is first sales email sending');
email.setPlainTextBody('This is a Test email Meaasege');
email.setSenderDisplayName('Raghavendr Reddy');
       
Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
msg.setToAddresses(toadd);
msg.setBccAddresses(tobccadd);
msg.setCcAddresses(toccadd);
msg.setSubject('This second email object message body');
msg.setHtmlBody('<h1>Hi Raghavendra</h1><font style:"color:red">This is html content for email body</font>');

       
        Messaging.SingleEmailMessage[] emails = new List<Messaging.SingleEmailMessage> {email,msg};
       
       
     
        Messaging.sendEmail(emails);
}
}


No comments:

Post a Comment