How to send email through salesforce apex?

 

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>

Need more help?

Hi there, was your problem or query resolved? If not & need more assistance, please do reach out to us at info@nanostuffs.com, we'll be more than delighted to help. Nanostuffs has 7+ years of extensive Salesforce & iOS/Android experience.
Holler Box