function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rocio VivancoRocio Vivanco 

How to use custom settings in APEX code for sending emails?

Hi all,

I have been trying to find out how can I use custom settings in apex code when it comes to use the sendEmail function.
For example, I have the following code lines:
global void sendEmail(){

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 // Assign the addresses for the To and CC lists to the mail object.
        // Specify the address used when the recipients reply to the email. 
        // Specify the name used as the display name.
        mail.setSenderDisplayName('Salesforce Email Reminder');
        // Specify the subject line for your email address.
        mail.setSubject('Yearly Email Reminder');
        // Set to True if you want to BCC yourself on the email.
        mail.setHtmlBody('This is a test email');
I have created a Custom Setting Test__c with the following fields and values in one record:

- ToAddress = ''
- replyToAddress = ''
- SenderDisplayName = 'Salesforce Email Reminder'
- Subject = 'Yearly Email Reminder'
- Body = 'This is a test'

Now, how can I replace the values in the parethesis for the respective email methods mail.setToAddresses(), etc. by the above mentioned custom setting. How can I invoke these custom setting values in the APEX code?. 

I would appreciate your answers.

Best Answer chosen by Rocio Vivanco
Please make address field as a string array
String toEmailAddress= cs.ToEmailAddress__c;
String[] sendTo = new String[]{toEmailAddress};

All Answers

Hey Rocio,

yes you can replace the hard coded values with the dyanmic values from custom setting , you can do this

Custom_Settings__c cs = Custom_Settings__c.getInstance('ToAddress');
String toAddr = cs.Email__C; // use your field's API name

and instead of this mail.setToAddresses(''); you can use 

Please mark the answer, if it helps you 
good luck 

v varaprasadv varaprasad
Hey Rocio,

Please check once following sample code : 
Test__c myCS1 = Test__c.getValues('EmailData');
String toAddress = myCS1.ToAddress__c;
String replyToAdd = myCS1.replyToAddress__c;
String senderDisplayName = myCS1.SenderDisplayName__c;
String subject = myCS1.Subject__c;
String body = myCS1.Body__c;


Hope this helps you!

@For Support:

Rocio VivancoRocio Vivanco
Thanks guys for your answers!!! :). Only two questions:

@varaprasad: On the part Test__c.getValues('EmailData'); what would be 'EmailData' in my case?.

For example, I created the custom setting with the fields. Then I created a record, when you create the record you have to give it a name, in my case the name is "List". Is that what you mean with 'EmailData'?

@the same manj_sfdc: Custom_Settings__c.getInstance('ToAddress');

Here you mean:


in my case?
Rocio VivancoRocio Vivanco
I got it. I am getting the error:

Method does not exist or incorrect signature: void setToAddresses(String) from the type Messaging.SingleEmailMessage

on the line:

when I try to replace it through the variable:
String toEmailAddress= cs.ToEmailAddress__c;

so, this way:

Can please somebody help me? what am I doing wrong here?​
Please make address field as a string array
Please make address field as a string array
String toEmailAddress= cs.ToEmailAddress__c;
String[] sendTo = new String[]{toEmailAddress};
This was selected as the best answer
Rocio VivancoRocio Vivanco
Thanks for responding. Now I am getting the same error for:


I declared the variable this way:

String [] replyToAddress = new String [] {cs.replytoAddress__c};

What am I doing wrong?
you dont have to use the array for replyToAddress pass it as just a  String
v varaprasadv varaprasad
You need to pass variable only like below   :


Rocio VivancoRocio Vivanco
Thanks varaprasad, before your answer I adjusted it and it worked.