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
tsailing.sfdctsailing.sfdc 

Apex emails not sent. No errors returned, success message received.

There's been a change in the Salesforce API that causes emails to not be sent when there are spaces around the email address string.

 

(Debug log message shown below)

This string of email addresses resulted in Email not sent -- ccAddresses: [tsailing.test1@gmail.com,  tsailing.test2@gmail.com,  tsailing.test3@gmail.com]

This string of email addresses resulted in Email sent -- ccAddresses: [tsailing.test1@gmail.com,tsailing.test2@gmail.com,tsailing.test3@gmail.com]

 

The main issue is that there will be NO ERRORS RETURNED OR DISPLAYED to inform users that their emails were not sent. In fact, the system WILL RETURN A SUCCESS MESSAGE.

 

Here's the debug message received -- (Messaging.SendEmailResult[getErrors=();isSuccess=true;])

 

This is NOT an acceptable API behavior.

 

 

Sample code used --

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

    String toRecipients = 'tsailing.test1@gmail.com';
    String[] toAddresses = toRecipients.split(';', 0);
    email.setToAddresses( toAddresses );
    System.debug('------------------- 7/1/2012: toAddresses: '+toAddresses);

String ccRecipients = 'tsailing.test2@gmail.com,  tsailing.test3@gmail.com,  tsailing.test4@hotmail.com';
    String[] ccAddresses = ccRecipients.split(',', 0); //switched this to "ccRecipients.split(', ', 0)" resolved the issue.
    email.setCcAddresses(ccAddresses);
    System.debug('------------------- 7/1/2012: ccAddresses: '+ccAddresses);
try {
    email.setSubject('test');
    email.setPlainTextBody('test');
    email.setBccSender(true);
} catch (Exception e) {
System.debug('------------------- 7/1/2012: e: '+e);
}

Messaging.SendEmailResult [] r = 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
System.debug('------------------- 7/1/2012: r: '+r);

 

Starz26Starz26

I just went through this with a client. took us all day to figure it out.

 

Was this documented somewhere?

 

I agree, not acceptable.

Starz26Starz26

We added this to the code after the split(',');

 

 

for(String a : variable)

   a = a.replace(' ','');

 

This will remove any spaces and still allow us to use the split ','

MarinaMartinMarinaMartin

THANK YOU for posting this. I was also going insane trying to troubleshoot a client problem. Removing spaces from the email string was the solution.

Gautam Manchanda 12Gautam Manchanda 12
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!
kamal somu akamal somu a
I had a different problem in receiving an email from salesforce. The reason is that I was trying to send a email from the constructor of a apex class. It seems we will not be able to send SingleEmailMessage from a constructor. I have followed this knowledge article (https://help.salesforce.com/articleView?id=000328515&type=1&mode=1) for resolving the issue.
Suraj Tripathi 47Suraj Tripathi 47

Hi,

you need to add '\\'  in your split function like 

 toRecipients.split('\\;', 0);

 Here is your Sample code:-
 
   Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
    
    String toRecipients = 'ankit.yadav@cloudanalogy.com';
    String[] toAddresses = toRecipients.split('\\;', 0);
    email.setToAddresses( toAddresses );
    System.debug('------------------- 7/1/2012: toAddresses: '+toAddresses);
    
    String ccRecipients = 'tsailing.test2@gmail.com,  tsailing.test3@gmail.com,  tsailing.test4@hotmail.com';
    String[] ccAddresses = ccRecipients.split('\\,', 0); //switched this to "ccRecipients.split(', ', 0)" resolved the issue.
    email.setCcAddresses(ccAddresses);
    System.debug('------------------- 7/1/2012: ccAddresses: '+ccAddresses);
    try {
        email.setSubject('test');
        email.setPlainTextBody('test body');
        email.setBccSender(true);
    } catch (Exception e) {
        System.debug('------------------- 7/1/2012: e: '+e);
    }
    
    Messaging.SendEmailResult [] r = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
    System.debug('------------------- 7/1/2012: r: '+r);

 

Thanks.

And don't forget to mark it best answer.