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
Srikanth Cheera 16Srikanth Cheera 16 

plz solve this How to send emails from apex class from custom object email id's using email template?

Khan AnasKhan Anas (Salesforce Developers) 
Hi Srikanth,

Greetings to you!

If you want to use a template to send emails then you need to use setTargetObjectId along with setTemplateId. Custom object Id isn't accepted in setTargetObjectId by Salesforce. Only User, Contact, Lead, or Person objects are allowed for targetObjectId. 

For workaround (maybe), please refer to the below link with a similar discussion which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/61552/send-email-with-template-on-custom-records-email-id


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Srikanth Cheera 16Srikanth Cheera 16
Then how to send custom object emailid
Khan AnasKhan Anas (Salesforce Developers) 
Hi Srikanth,

You can send emails to custom object emails easily if you don't want to use a template.
 
public with sharing class SendMailCustomObjectController {

    public void sendMassMail() {
        List<Candidate__c> lstcon = [SELECT id, Name, Email__c FROM Candidate__c WHERE Email__c != NULL ];
        String cemail;
        for(Candidate__c cd:lstcon) {
            cemail = cd.Email__c;
         
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.toAddresses = new String[] { cemail };
            mail.subject = 'Subject Test Message';
            mail.plainTextBody = 'This is a test mail';
            Messaging.SingleEmailMessage[] mailss =   new List<Messaging.SingleEmailMessage> {mail};
            Messaging.SendEmailResult[] results = Messaging.sendEmail(mailss);
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Srikanth Cheera 16Srikanth Cheera 16
please use EmailTemplate Thanks, Srikanth Cheera
Raj VakatiRaj Vakati
Try like below
 
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Waitlisted opportunity now open'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id); 

   mail.setWhatId(REcordId);  ​
mail.setSaveAsActivity(false);
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail);
Messaging.sendEmail(allmsg,false);

 
Ajay K DubediAjay K Dubedi
Hi Srikanth,

Below query can fulfill your requirements. Hope this will work for you.

List<Messaging. MassEmailMessage > messagesToSend = new List<Messaging. MassEmailMessage>();

        for (Id objectId : dataExtractRequestIds) {
            Messaging. MassEmailMessage mail = new Messaging. MassEmailMessage();

            mail.setTargetObjectIds(userIds);
            mail.setTreatTargetObjectAsRecipient(true);

            mail.setTemplateId(templateId);
            mail.setwhatId(objectId);
            mail.setSaveAsActivity(false);

            messagesToSend.adD(mail);
        }

        Messaging.sendEmail(messagesToSend);
      
Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi