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
NiviNivi 

default 'toAddress' in Messaging.SingleEmailMessage() API

Hello All,
I have written a code for sending email through APEX. 
I am using a custom field for saving original Email address from Email field of Contact object. And masking the original Email. So my original email field value is 'DuXXX@XXXXX.XXX' and Custom is like 'Dummy@gmail.com'. 
When i am sending the email using Messaging.SingleEmailMessage() API the when i receive email in 'to address' ther are both the values (from custom email field and standard one.) 
How can i remove the standard one from there as i have only added 1 value from custom field in code.
Or it is standard salesforce behaviour.

Thanks,
Nivedita 
Meghna Vijay 7Meghna Vijay 7
Hi Nivedita,

String[] toAddresses = new List<String>{contact.CustomFieldAddress};
email.setToAddresses(toAddresses);

Hope it helps, if it does mark it as solved.

Thanks
NiviNivi
Hello Maghna,
Thank you for your reply but i am doing this and i don't know how in email there are two addresses. Although i am adding only one custom field value.
My Code :
String email =null;
            if(con.RecordTypeId == archiveRecordTypeId){
             email=con.Email_Address__c  ; 
            }else{
             email =con.Email;   
            }

         if(email != NULL && con.Id != NULL){             
            String[] toAddresses = new String[] {email}; 
            mail.setToAddresses(toAddresses);
            mail.setReplyTo(System.Label.CON_Email_Address);
            mail.setBccSender(false);
            mail.setTemplateId(templateId.Id);
            mail.setWhatid(con.Id);
            mail.setSaveAsActivity(false);
            mail.setTargetObjectId(con.Id);
            mails.add(mail);

            Messaging.sendEmail(mails);
        }
Also , Below is the To Address that i received.
To Address in Email

Thanks
Meghna Vijay 7Meghna Vijay 7
Hi Nivedita,

Can you post the code before String email =null; as i want to know the loop you are using . Is it a query loop or just iteration of list?

Thanks
NiviNivi
Hello Meghna,
There is no loop or iteration. I am sending single email.
Also 'email' is a variable that i used in my code.

Thanks,
Nivedita
Meghna Vijay 7Meghna Vijay 7
Hi Nivedita ,

This is what i did in my org:-
Contact con = [SELECT Email, Email_Address__c FROM Contact WHERE Id='0030I000021jKrIQAU'];
String email = '';
if(con.Email_Address__c != null) {
   email = con.Email_Address__c; 
}else {
    email = con.Email;
}

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
String[] toAddresses = new String[] {email}; 
    //System.assert(false, toAddresses);
            mail.setToAddresses(toAddresses);
            //mail.setReplyTo(System.Label.CON_Email_Address);
            //mail.setBccSender(false);
            mail.setPlainTextBody('Hello');
            //mail.setTemplateId(templateId.Id);
            mail.setWhatid(con.Id);
            mail.setSaveAsActivity(false);
            //mail.setTargetObjectId(con.Id);
            mails.add(mail);

            Messaging.sendEmail(mails);
System.debug('mails===='+ mails);

Basically when i commented mail.setTargetObjectId then it was not fetching standard email of contact and i added it in Email_Address__c.

User-added image
Hope it helps, if it does mark it as solved.

Thanks