How to send email through salesforce apex?

H

 

Class Code:

public class clsTest
{
public Boolean emailSent { get; set; }
public void sendMail(string email){

Messaging.SingleEmailMessage mailHandler = new Messaging.SingleEmailMessage();

String[] emailRecipient = new String[]{email};
//set the recipient
mailHandler.setToAddresses(emailRecipient);

//set the reply email address
mailHandler.setReplyTo('juilee.joshi@nanostuffs.com');

//set the display name
mailHandler.setSenderDisplayName('Juilee');

//set the subject
mailHandler.setSubject('Hi');

//set the template ID
//mailHandler.setTemplateId('00X200000015XFL');

mailHandler.setHtmlBody('Hi');

try
{
Messaging.sendEmail(new Messaging.Email[] { mailHandler });
emailSent = true;
}
catch(EmailException e)
{
System.debug(e.getMessage());
emailSent = false;
}

}
}

Page Code:

<apex:page controller="clsTest" action="{!sendMail}" title="Email Sending">
   <apex:outputText value="{!emailSent}" />
</apex:page>

About the author

juilee.joshi
By juilee.joshi

Category