0

I'm trying to send mails to the owners of the Campaigns on certain conditions using SingleEmailMessage. While i could see no error in debug the mails seems not being recieved @ my inbox. Below the code snippet....any inputs to correct this or troubleshoot please?

public static void sendMail(List<Campaign> Campaigns){
    for(Campaign camp:campaigns){
        //New instance of a single email message
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // Recipient
        mail.setTargetObjectId(camp.CreatedById);
        // Set the mail template
        mail.setTemplateId('00X26000000DbPe');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setSaveAsActivity(false);
        //mail.setWhatid(camp.Id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Nb: I'm trying to trigger the mail by testing it using the annotation @isTest for the class...

  • 2
    if you are using @isTest then nothing will be sent. No actual emails are sent during test methods. Use Dev console to execute the class and send the email – Eric Oct 8 '15 at 18:12
  • Thought experiment -- imagine if testmethods would actually send email. Result of thought experiment: "Much, much, much unwanted spam and worse, false positive emails sent to real people indicating orders fulfilled, accounts cancelled, etc - and repeated on every testmethod execution" – cropredy Oct 8 '15 at 18:23
  • n.b. triggers that send email messages are likely to run into governor limits as you are limited on # outbound apex emails PER DAY across whole org. Much better to set boolean in sobject and let workflow send email alert – cropredy Oct 8 '15 at 18:25
  • @crop1645 - Thought Experiment - consider that phrase stolen.....So many well placed opportunities to use it.....Oh and what is "n.b" ? – Eric Oct 8 '15 at 18:53
  • 1
    @Eric - n.b. = nota bene == "by the way". english.stackexchange.com/questions/103318/…. Although it appears I have been not capitalizing it per rules: N.B. – cropredy Oct 8 '15 at 21:35
1
  1. @IsTest methods will never send emails. Thought experiment -- imagine if testmethods would actually send email. Result of thought experiment: "Much, much, much unwanted spam and worse, false positive emails sent to real people indicating orders fulfilled, accounts cancelled, etc - and repeated on every testmethod execution"
  2. You are hard-coding the email templateId -- this is not best practice as the template Id in sandbox will not be the same when you deploy to PROD. Better to query for the template's Id from Sobject EmailTemplate
  3. Using apex outbound email may hit a governor limit as there is an org wide DAILY limit across all transactions - check Limits guide for latest maximimum; Winter 16 = 1000. Better solutions involve setting a boolean field on the target object and then having a workflow/process builder use email alerts and field update (to reset the boolean). Or, here are some other alternatives. If you insist on using outbound apex email, you should use the reserveSingleEmailCapacity() method to avoid getting a limits error and also providing for some graceful alternative for the transaction.
  • they killed the Workaround using workflow email alerts. They are now limited to 1,000 per 24 hours per license (has this limit always been there? IIRC the limits doc used to say unlimited for WF) – Eric Oct 11 '15 at 2:46
  • @Eric -- really? I interpreted this statement ` Single emails sent using the Salesforce application don't count toward this limit` as meaning that workflow-based email alerts are not bound by the 1000 limit. Also - this help article states 1000 email alerts per user license: help.salesforce.com/HTViewHelpDoc?id=workflow_email_limits.htm - so 100 user licenses = 100,000 email alerts per day – cropredy Oct 11 '15 at 4:41
  • @Eric -- BTW, I think you should really get credit for the answer here as your first comment to the OP was the right answer - post an answer and hopefully OP will mark it as the 'answer' – cropredy Oct 11 '15 at 4:43
  • Yea, but for those small orgs with only says 1 admin that do email functionality for their users (say sending renewal emails for large companies) it presents a problem - and no worries on the credits, main goal is that the OP learns something from it and other can as well. Your write up is much more details. Plus you should get mucho point for "Thought Experiment" lol - Besides, it does not look like the OP is going to mark the answer as accepted just like so many other one time posters – Eric Oct 11 '15 at 5:29
  • @Eric -- hadn't thought about your use case - few users, many emails. It is hard sometimes to work around SFDC's innate goal of getting one's org to buy more user licenses – cropredy Oct 12 '15 at 17:32

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.