5

Below is my sendEmail Method .

public void sendMail(List<user> Users, string emailTemplateName) {
    final string FROM_ADDRESS ='Test';
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage email;
    if(emailTemplateName == DISABLE_NOTIFICATION_WARNING){
        EmailTemplate emailTempRec = getEmailTemplate(emailTemplateName);
        for(User userRec : Users){
            email = new Messaging.SingleEmailMessage(); 
            email.setTargetObjectId(userRec.Id);
            email.setSenderDisplayName(FROM_ADDRESS); 
            email.setSaveAsActivity(false); 
            email.setTemplateId(emailTempRec.Id);
            emails.add(email);
        }
    }else if(emailTemplateName == ADMIN_NOTIFICATION){
        email = new Messaging.SingleEmailMessage();
        final string SUBJECT_TO_ADMINS = 'Account disabled due to inactivity';
        String[] toAddresses = new String[] {label.AccountAdmins}; 
        string todaysdate = String.valueOf(Date.today()).removeEnd(' 00:00:00');
        String htmlBody = 'Hello,<p/>The following individual accounts were disabled due to inactivity on '+todaysdate+'.<br/>';
        htmlBody += '<table style="width:50%"> <tr> <th style="text-align: left">Name</th>';
        htmlBody += '<th style="text-align: left">Federation ID</th> </tr>';
        for (User userRec : Users){
            htmlBody += '<tr> <td style="width:50%">'+userRec.name;
            htmlBody += '</td> <td style="width:50%">'+ userRec.FederationIdentifier + '</td> </tr>';
        }
        htmlBody += '</table>';
        email.setHtmlBody(htmlBody);
        email.setToAddresses(toAddresses);
        email.setSubject(SUBJECT_TO_ADMINS);
        email.setSenderDisplayName(FROM_ADDRESS); 
        email.setSaveAsActivity(false); 
        emails.add(email);
    }
    List<Messaging.SendEmailResult> 
    results = Messaging.sendEmail(emails);  
    System.debug('--->results.size()='+results.size());
    System.debug('--->results[0].success='+results[0].success);       
    System.debug('--->results[0].getErrors().size='+results[0].getErrors().size());
    System.assertEquals(3, results.size());
    System.assertEquals(true, results[0].success);
    
}

I tried following ways to debug this issue.

  1. Checked org Email deliverabilty setup , it's set to 'ALL Email'.
  2. Ran this code in my personal sandbox, it works like a charm. But company org the issue still exists.
  3. when i disabled the 'Email security complaiance'in comapny org, i did recieve emails(but this not a acceptable solution, as production setup is different)
  4. Tried to run this code in different org similar to production setup(Where they have setup DKIM keys) same issue, No emails are being sent.

Is there any alternate way this issue can be resolved?

2
  • Well researched, organized, and formatted first post. Welcome to SFSE!
    – Adrian Larson
    Jun 8, 2021 at 3:41
  • 1
    An important troubleshooting step that is missing from your sequence is verifying whether a message was actually sent by Salesforce mail transfer agent (MTA) and (if so) what was the response from the recipient's mail service. To do this, you have to request email logs. Please update your question with the results of this step.
    – identigral
    Jun 8, 2021 at 16:25

1 Answer 1

0

I had same issue, you could try to use setOrgWideEmailAddressId to implement from Address. Because Gmail bounced emails from automated process. You can check it by using:

SELECT IsBounced 
FROM EmailMessage 

Documentation

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .