+ Start a Discussion
pranav shah 40pranav shah 40 

mass email messaging using apex class

Hi all,

I need to send mail to multiple email id ..different person whose email id will be external// ie not present in salesforce org...
I dont want to hardcord the id//.. just wanto fetch it thorugh custom metadata

How it will be done


List<CEMReportConfig__mdt> cemReportConfigLst = [SELECT Id,MasterLabel,ToEmailAddresses__c,CcEmailAddresses__c,EmailUniqueTemplateName__c FROM CEMReportConfig__mdt];
                                                                 
    
    //Messaging.MassEmailMessage email = new Messaging.MassEmailMessage();
    
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        
    efa.setFileName('Order.xml');
    efa.setBody(csvBlob);
    String[] toAddresses = new String[] {'pranav17shah@gmail.com'};
        
    email.setSubject('Order Data');
    email.setToAddresses(toAddresses);
    email.setPlainTextBody('Batch Processed');
    
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  


Regards
Pranav Shah
Raj VakatiRaj Vakati
Try like this
 
CEMReportConfig__mdt cemReportConfigLst = [SELECT Id,MasterLabel,ToEmailAddresses__c,CcEmailAddresses__c,EmailUniqueTemplateName__c FROM CEMReportConfig__mdt Where MasterLabel='Default_EMail'];
                                                                 
   Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        
    efa.setFileName('Order.xml');
    efa.setBody(csvBlob);
	List<String> strToEMail = cemReportConfigLst.ToEmailAddresses__c.split(',');
		List<String> ccToEMail = cemReportConfigLst.CcEmailAddresses__c.split(',');
		        EmailTemplate template =  [SELECT Id, Name FROM EmailTemplate WHERE DeveloperName = cemReportConfigLst.EmailUniqueTemplateName__c LIMIT 1];

 
        
    email.setSubject('Order Data');
	        email.setTemplateId(ORDER_ID);

    email.setToAddresses(strToEMail);
	email.setCcAddresses(ccToEMail);
    email.setPlainTextBody('Batch Processed');
     emails.setTemplateId(template.Id);

    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});