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
Clap MasterClap Master 

Never receiving mail sent with Messaging class

I'm making a simple class to send emails out, but I never receive any of the emails my class sends.  I'm in a sandbox, and executing the class via a test class (annotated with 

(seeAllData=true)).  Messaging.sendEmail() is returning 'true'.  

 

        				Messaging.reserveSingleEmailCapacity(2);
        				Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        				String[] toAddresses = new String[] {c.contact.Email};
        				mail.setToAddresses(toAddresses);
        				mail.setReplyTo('support@mysite.com');
        				mail.setSenderDisplayName('MySite Support');
        				mail.setSubject('Case #' + c.CaseNumber + ' is awaiting an update from you');
        				mail.setHtmlBody('Hi ' + c.contact.FirstName + ' - ' +
        				'<br><br>' +
        				'The above referenced case number is open and we are awaiting an update from you.  ' +
        				'Please either reply to the original email thread for this case or update your case by logging in to the ' +
        				'<a href=http://support.mysite.com/>MySite Support Portal</a> ' +
        				'and let us know how things are going.' +
        				'<br><br>' +
        				'Regards,<br>MySite Support');
        				List<Messaging.SendEmailResult> result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        				System.debug('result -> ' + result.get(0).isSuccess());

 I changed the compant name to mysite.  Also, in the debug, I see that the recipient email address is in the EMAIL_QUEUE line, and it still fails even if I hard code a recipient email address.

 

Any ideas?

TrinayTrinay

Hi Clap,

 I thing there is no issue in your code. Please check your class in Debug logs. and please post what kind of errors did u get.

 

Otherwise refer the following class, its also used to send a mail to the contact.

 

public class Email
{
private Contact cont;
public Email(ApexPages.StandardController controller)
{
this.cont=(Contact)controller.getRecord();
}

  public void SendEmail()
  {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTargetObjectId(cont.Id);
    mail.setTemplateId('your template id here');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  }

}

 

I hope this link will helpful to you.

 

 

Clap MasterClap Master

I get no errors in the debug logs.  One odd thing, though, is that it shows this:

 

Number of Email Invocations: 0 out of 10

 

despite the fact that Messaging.sendEmail() returns true

crop1645crop1645

testmethods will never send out emails; SFDC disables this.  You can get emails if you test from the UI.

 

The reason for this (I think) is to prevent regression testing from sending out emails to production users.

 

 

Clap MasterClap Master

You mean by clickomg 'Run Test' from the Apex classes view, as opposed to testing from my IDE?  Or do you mean from an implemented page in my sandbox?  I'm actually running this as a scheduled batch job, so I have no UI to test fromm  Is that what you mean?

crop1645crop1645

Clap Master

 

Outbound emails will not be sent if the test execution is from any testMethod execution - regardless of how that testmethod is launched:

 

* Apex Run Tests in Eclipse

* Run Tests in Force.com UI

 

When I was testing my batch APEX job and I wanted to ensure that the emails were sent and they looked 'good', I wrote a small VF controller/page that enabled me to launch my batchable class.  You could also do this via anonymous Apex execution through the developer console