+ Start a Discussion
vepakvepak 

check whether setCcAddresses IS NULL OR NOT

Hi There,

 

I'm using visualforce page to specify to email address and cc email address to send email.

 

If both to address and cc address are specified my class handling it and working fine.

 

But cc email address field is blank in visualforce page my class coudn't handle and throws error

 

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : : []

 

NOTE: I ASSUME ONLY ONE EMAIL IS SPECIFIED IN toemail FOR THIS EXAMPLE CODE 

 

public String ccemail {get;set;} 
public String toemail {get;set;}
public String subject {get;set;}
public String body {get;set;}
string[] ccaddress;


if(ccemail!=NULL)
 ccaddress=ccemail.split(',', 0);
    else
    ccaddress=NULL;
    

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setUseSignature(false);
    mail.setToAddresses(new string[]{toemail});
    
    
    mail.setCcAddresses(ccaddress);
    
    mail.setSubject(subject);
    mail.setHtmlBody(body);
     
 

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 


Could you please help me in this.

 

Thanks,

Vamsi.

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

I think this will solve it, you can not check ccaddress != '' as ccaddress is an array

 

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setUseSignature(false);
    mail.setToAddresses(new string[]{toemail});
    
    if(ccemail!=NULL && ccemail.trim() != '')
    {
     ccaddress=ccemail.split(',', 0);
     mail.setCcAddresses(ccaddress);
    }
    mail.setSubject(subject);
    mail.setHtmlBody(body);
     

 

 

All Answers

Ankit AroraAnkit Arora

Is there any problem if condition before the line where you are adding in CC address :

 

if(ccaddress != null && ccaddress.size() > 0)
    mail.setCcAddresses(ccaddress);

 Or what exactly you are expecting?

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

I think this will solve it, you can not check ccaddress != '' as ccaddress is an array

 

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setUseSignature(false);
    mail.setToAddresses(new string[]{toemail});
    
    if(ccemail!=NULL && ccemail.trim() != '')
    {
     ccaddress=ccemail.split(',', 0);
     mail.setCcAddresses(ccaddress);
    }
    mail.setSubject(subject);
    mail.setHtmlBody(body);
     

 

 

This was selected as the best answer
vepakvepak

Thanks Shashi, it worked.

vepakvepak

Ankit, I think shashi is correct, I also tried same but ddn't worked.

 

Thanks a lot  for your time.

Ankit AroraAnkit Arora

I think I have corrected my answer, but good that you have the answer :)

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page