You need to sign in to do that
Don't have an account?

Email bounce for Apex scheduler
Hello All,
Below apex class sends email to the specific contacts on a monthly basis.I want to know if there is any email bounce due to incorrect email.I have updated the settings in Email Deliverability to return the bounced emails to sender.However it is not working i.e., I don't get any email if I provide invalid email.Is there any way to know for which contacts the email is not delivered.I don't want to use email logs as the requirement is to update a date field on contact whenever email is delivered successfully.
Global class SendEmailToAccount implements Schedulable
{
global void execute(SchedulableContext sc)
{
sendmail();
}
public void sendmail()
{// Get default sender email address
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress where DisplayName='Sender' limit 1];
//Get Email Template
EmailTemplate templateId = [Select id from EmailTemplate where name = 'TemplateName'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
Set<Id> cid= new Set<id>();
List<Account> accList = [select Id,primary_contact__c from Account where RecordType.developerName = 'My_Account_Record_Type'];
List<Messaging.SendEmailResult> results;
for(account acc:accList)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id);
mail.setSaveAsActivity(false);
mail.setTargetObjectId(acc.Primary_Contact__c);
mail.setWhatId(acc.Id);
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail);
}
if(allmsg.size()>0)
{
results = Messaging.sendEmail(allmsg,false);
}
if (!results.get(0).isSuccess())
{
System.StatusCode statusCode = results.get(0).getErrors()[0].getStatusCode();
String errorMessage = results.get(0).getErrors()[0].getMessage();
system.debug(errorMessage);
}
}
}
Below apex class sends email to the specific contacts on a monthly basis.I want to know if there is any email bounce due to incorrect email.I have updated the settings in Email Deliverability to return the bounced emails to sender.However it is not working i.e., I don't get any email if I provide invalid email.Is there any way to know for which contacts the email is not delivered.I don't want to use email logs as the requirement is to update a date field on contact whenever email is delivered successfully.
Global class SendEmailToAccount implements Schedulable
{
global void execute(SchedulableContext sc)
{
sendmail();
}
public void sendmail()
{// Get default sender email address
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress where DisplayName='Sender' limit 1];
//Get Email Template
EmailTemplate templateId = [Select id from EmailTemplate where name = 'TemplateName'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
Set<Id> cid= new Set<id>();
List<Account> accList = [select Id,primary_contact__c from Account where RecordType.developerName = 'My_Account_Record_Type'];
List<Messaging.SendEmailResult> results;
for(account acc:accList)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id);
mail.setSaveAsActivity(false);
mail.setTargetObjectId(acc.Primary_Contact__c);
mail.setWhatId(acc.Id);
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail);
}
if(allmsg.size()>0)
{
results = Messaging.sendEmail(allmsg,false);
}
if (!results.get(0).isSuccess())
{
System.StatusCode statusCode = results.get(0).getErrors()[0].getStatusCode();
String errorMessage = results.get(0).getErrors()[0].getMessage();
system.debug(errorMessage);
}
}
}
Please refer below Salesforce help document if that can help you in some way.
Why does bounce management not work sometimes?
https://help.salesforce.com/articleView?id=Why-does-bounce-management-not-work-sometimes-1327109130633&type=1&language=en_US
Hope this helps you!
If this helps you, please mark it as solved.
Thanks and Regards
Sandhya