0

We have a future class which will call future class to send mail. Sometimes I get this mail from salesforce.

Apex script unhandled exception by user/organization: 005A00000062Q6O/00DA0000000YXoL Failed to invoke future method 'public static void sendEmail(Set)' on class 'AsyncApexClass' for job id '707A000004xKn9u' caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, From: invalid email address: 'Certification Support' <test@external.abc.com>: [From__c] Class.AsyncApexClass.sendEmail: line 43, column 1

Below is my future class:

    global class AsyncApexClass{
@future

public static void sendEmail(Set<Id> sendList){
List <Notes__c> notesList = new List<Notes__c>();
notesList.clear();
for(List<EmailMessage> emailmsglist:[select Id,parentId,Parent.Email__c,Parent.Contact.Email,ToAddress, FromAddress, Subject, TextBody, HTMLBody, CreatedDate from EmailMessage where id in :sendList] )
{
        for(EmailMessage emlist :emailmsglist){

             Notes__c note= new Notes__c(); // Create a note object 
             note.Case__c= emlist.parentid;
                 IF (emlist.HTMLBody != NULL && emlist.HTMLBody != ''){
                 note.Message__c = emlist.HTMLBody;
                 }
                 else {
                 note.Message__c = emlist.TextBody;
                 }    
             note.Sent_To__c = emlist.ToAddress;
             note.From__c = emlist.FromAddress;
             note.Subject__c = emlist.Subject;
             note.Datetime_Created__c = emlist.CreatedDate;
             if (emlist.Parent.Contact.Email == emlist.ToAddress || emlist.Parent.Email__c == emlist.ToAddress) // If the email is the same as Case's contact email or Email__c on Case itself, the type is a response.
             {
             note.Type__c = 'Response';
             }
             else {
             note.Type__c = 'Forward/Others';
             }
             notesList.add(note); // Add note object to the list
         }
}
// If the note list has records, insert the list.
if(notesList.size()>0) {
       insert notesList;
       }
}
}
0

I believe it is due to what Salesforce interprets as valid address. From this article

For the local part of the email address we accept the following characters. The local part is anything before the @ sign.

abcdefg.hijklmnopqrstuvwxyz!#$%&'*/=?^_+-`{|}~0123456789

Note: The character dot “.” is supported; provided that it is not the first character in the local-part

For the domain part of the email address we accept. The domain part is anything after the @ in an email address:

0-9 and A-Z and a-z and dash “-“

Example email address: abcdefg.hijklmnopqrstuvwxyz!#$%&'*/=?^_+-`{|}~0123456789@acme-inc.com

The following note.From__c = emlist.FromAddress; is storing a wrong format (accordign to salesforce) i.e. test@external.abc.com.

  • I changed the email format as per your suggestion. Still I am getting the same error. – AJoshi Jun 6 '18 at 6:21
  • Can you update the question with your error? – manjit5190 Jun 6 '18 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.