+ Start a Discussion
kyasukyasu 

How to fail to send e-mail

Hi,

I am writing "send e-mail program" using sendEmail method.

 

I want to see error message about failure of sending e-mail.

 

How can I make e-mail to be failure ?

Best Answer chosen by Admin (Salesforce Developers) 
SatgurSatgur

Hi,

 

You can simulate error in send Email by using one of the following approaches -

 

1. Populate incorrect, incomplete data in SingleEmailMessage instance

    Note - All email must have a recipient value of at least one of the following:
         • toAddresses
         • ccAddresses
         • bccAddresses
         • targetObjectId
         • targetObjectIds

 

 For test purpose, don't specify any of the above attributes of SingleEmailMessage object.

 

Now run the code and sendEmail should fail with an error.

 

2. Governor limits on Send Mail invocation

      you can only call sendMail 10 times within Apex code. For test purpose, you can make your code call sendEmail more than 10 times.

 

 

 

All Answers

Pradeep_NavatarPradeep_Navatar

For your own custom messages, use message classes :

 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Invalid mail');              

ApexPages.addMessage(myMsg);

 

and put this component <apex:pagemesseges> in VF Page. 

 

Did this answer your question? if so, please mark it solved.                              

kyasukyasu

thnak you for reply

and

Sorry for non clear-cut explanation

 

I wrote

 

//send e-mail
Messaging.sendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});

//error
for ( Messaging.sendEmailResult result : results ) {
    if ( !result.isSuccess () ) {
        Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.ERROR, 'my error msg');
        Apexpages.addMessage(msg);
    }

 

But, I have no idea how to be error situation.

 

SatgurSatgur

Hi,

 

You can simulate error in send Email by using one of the following approaches -

 

1. Populate incorrect, incomplete data in SingleEmailMessage instance

    Note - All email must have a recipient value of at least one of the following:
         • toAddresses
         • ccAddresses
         • bccAddresses
         • targetObjectId
         • targetObjectIds

 

 For test purpose, don't specify any of the above attributes of SingleEmailMessage object.

 

Now run the code and sendEmail should fail with an error.

 

2. Governor limits on Send Mail invocation

      you can only call sendMail 10 times within Apex code. For test purpose, you can make your code call sendEmail more than 10 times.

 

 

 

This was selected as the best answer
kyasukyasu

Thank you for reply

 

I can get error

 

Satgur, thank you very much.