Send email using apex

How to send email using apex
How to send email using apex

Here is to code to send email using salesforce apex when new contact record is get created.

				
					List<Messaging.Email> emailList  = new List<Messaging.Email>();
        Messaging.SingleEmailMessage emailTemplate = new Messaging.SingleEmailMessage();
            List<Contact> lstNewContact = [Select Name, Email From Contact where Id =: Trigger.new ];
            for(Contact contact : lstNewContact) { 
                emailTemplate.setToAddresses(new String[] {contact.Email});
                emailTemplate.setSubject('New contact record created');
                emailTemplate.setHtmlBody('Hello team, new contact is added Name:  '+ contact.Name);
                emailList.add(emailTemplate);
                Messaging.sendEmail(emailList );
            }
        }
				
			

We have explained the same in this video.

Leave a Reply