Send Email In Apex Class

by Rijwan Mohmmed
0 comment
send-email-in-apex-class-Salesforce

Hello, friend today we will learn to send email in apex class. There are already defined email classes in apex, so we just use this class to send emails. There are multiple variables in email class like address, body, subject, etc.

Code & Process :

Here we will discuss two types of send email to class

  1. With Email Template
  2. Without Email Template

With Email Template: In this part, we will write a code in which we use an email template to send an email

public static void SendMails(List<Contact> Contacts){
    List<Messaging.SingleEmailMessage> massListMails = new List<Messaging.SingleEmailMessage>();
        for(Contact con : Contacts){

            if(con.Id!=null){
               //Set list of people who should get the email
                String[] toAddresses = new String[] {con.Email};
	            List<String> ccTo = new List<String>();
                ccTo.add('rijwanmohmmed@gmail.com'); 
		        // (Optional) Set list of people who should be CC'ed
                mail.setCcAddresses(ccTo);
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
		        // set list of email address
                email.setToAddresses(toAddresses); 
		        // do not save email as activity   
                email.setSaveAsActivity(false);
		        email.setReplyTo('rijwanmohmmed@gmail.com'); //Set who the email is sent from
                email.setSenderDisplayName(con.Lastname); //not required
	            email.setWhoId(con.Id); //use whatid when object are not lead or contact
		        email.setTargetObjectId( UserInfo.getUserId() );

		        // set the sender email
                email.setOrgWideEmailAddressId([SELECT Id FROM OrgWideEmailAddress WHERE Address = 'do_not_reply@example.com' LIMIT 1].Id);

		        // set the email template id
                email.setTemplateId( [SELECT Id from EmailTemplate where developername='emailtemplatename'].Id);
                massListMails.add(email);
            }
        }

        if(!massListMails.isEmpty()){
            Messaging.sendEmail(massListMails);
        }
 }

Without Email Template: In this part, we will write code in which we create a body and subject for the email and send the email

public static void SendMails(List<Contact> Contacts){
     List<Messaging.SingleEmailMessage> massListMails = new List<Messaging.SingleEmailMessage>();
     for(Contact con : Contacts){

         if(con.Id != null){
            String[] toAddresses = new String[] {con.Email}; //list of people who get the email
	        List<String> ccTo = new List<String>();
                ccTo.add('rijwanmohmmed@gmail.com'); //Set list of people who should be CC'ed
                email.setCcAddresses(ccTo);

                String body='<html><body><p>test body</p><br /><br />';    
                body +='</body></html>';

                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setToAddresses(toAddresses); // set list of email address
	            email.setReplyTo('rijwanmohmmed@gmail.com'); //Set who the email is sent from
                email.setSenderDisplayName(con.Lastname); //not required
                email.setSubject('subject');
                email.setHtmlBody(body);
                massListMails.add(email);
            }
        }

        if(!massListMails.isEmpty()){
            Messaging.sendEmail(massListMails);
        }
 }
What’s your Reaction?
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.