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
Gautam Manchanda 12Gautam Manchanda 12 

Apex Emails not sent out using Single Email Class

Hi,

I am also facing quite similar issue. I am sending emails using the below code:

for(Payment__c pay : lstOfPaymentsToProcess) {
      EmailMessage emailMsg = new EmailMessage();
      DateTime dt = null;
      String approvalDate = '';
      if((mapOfOppIdAndOpp.get(pay.CSAF_Number__c).Approval_Date__c)!= null) {
       approvalDate = Date.newInstance((mapOfOppIdAndOpp.get(pay.CSAF_Number__c).Approval_Date__c).year(), (mapOfOppIdAndOpp.get(pay.CSAF_Number__c).Approval_Date__c).month(), (mapOfOppIdAndOpp.get(pay.CSAF_Number__c).Approval_Date__c).day()).format();
      }        
      dt = pay.Date__c;
      Date payDate = Date.valueOf(dt);
      String[] ccAddresses = new String[]{'Fffinance@hindustantimes.com',mapOfOppIdAndOpp.get(pay.CSAF_Number__c).Owner.Email};
      string emailHtmlBody = 'Hi All';
      Messaging.SingleEmailMessage singleEmailMsg = new Messaging.SingleEmailMessage();
      singleEmailMsg.setTargetObjectId(mapOfOppIdAndOpp.get(pay.CSAF_Number__c).Client__r.Key_Contact_Person__c);
      singleEmailMsg.setCcAddresses(ccAddresses);
      singleEmailMsg.setSubject('Cheque bounced against ' + mapOfOppIdAndOpp.get(pay.CSAF_Number__c).CSAF_No__c);
      singleEmailMsg.setHtmlBody(emailHtmlBody);
      lstOfMails.add(singleEmailMsg);
    }
    Messaging.sendEmail(lstOfMails);


All works good some times but some times it fails to send emails. Now this is a big issue for me to debug for the client as this is an intermittent issue and is not being reported daily. What can be the possible cause of it and any suggestions to resolve this would be of great help!

​Thanks in Advance!