Site icon WedgeCommerce

How to send Email from APEX code

Being a developer, one would have had felt the need to send an email from their own code. Various platform might or might not offer methods to achieve this task, however APEX provides us classes to compose email messages. This is what I am going to demonstrate you, how to send email from APEX.

APEX Class

The Code for sending Email is as follows:

public class emailSending {
    public string toMail { get; set;}
    public string ccMail { get; set;}
    public string repMail { get; set;}
    
	public void sendMail(){
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        string[] to = new string[] {toMail};
        string[] cc = new string[] {ccMail};
        
        email.setToAddresses(to);
        if(ccMail!=null && ccMail != '')
	        email.setCcAddresses(cc);
        if(repmail!=null && repmail!= '')
        	email.setInReplyTo(repMail);
        
        email.setSubject('Test Mail');
        
        email.setHtmlBody('Hello, <br/><br/>This is the test mail that you generated. <br/>The Email Id for which this mail was generated by '+toMail+'<br/><br/>Regards<br/> Developer');
        try{
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        }catch(exception e){
            apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getMessage()));
        }
        
        toMail = '';
        ccMail = '';
        repMail = '';
    }
}

In this class we have used the standard email class of APEX for single messaging, i.e. Messaging.SingleEmailMessage. This class contains most of the functionality required to send a single email message to one or more than one person.

The setToAddresses() function sets the to field of the email. This function takes a string list as an input and hence we have created a string list object and added the to address to that string. In this example I have provided only one email address. You can take multiple address as input separating them with semi-colon and then adding all the addresses to list.

The setCcAddresses() function works in the same way, however it sets the CC field of email instead of to field. Same goes for setInReplyTo() function with only one difference that it takes a string as argument.

The setSubject() function sets the subject of the mail and the setHtmlBody() sets the body of the mail in HTML form. If you want to set body in text form then use the function setPlainTextBody() function.

To send the mail simply use the sendEmail() function of the Messaging class.

VF Page Code

The Code for my VF page is something like this:

<apex:page controller="emailSending">
    <p>
        Test Email Sending:
    </p>
    <apex:form>
        <apex:outputLabel value="Send Mail To: " for="To"/>
        <apex:inputText value="{!toMail}" id="To"/><br/>
        <apex:outputLabel value="CC Mail To: " for="CC"/>
        <apex:inputText value="{!ccMail}" id="CC"/><br/>
        <apex:outputLabel value="Reply Mail To: " for="rep"/>
        <apex:inputText value="{!repMail}" id="rep"/><br/>
        <apex:commandButton action="{!sendMail}" value="Send Email"/>
    </apex:form>
</apex:page>

As you can see that the VF page has normal tags and will be used only to get the data from the user.

Output

Support

That’s all for how to send email from APEX, for any further queries feel free to contact us at:

https://wedgecommerce.com/contact-us/

Or let us know your views on how to make this code better, in comments section below.

Exit mobile version