Concepts or coding lessons of Salesforce that you can implement easily

Learning Send Mass Emails From Your Apex Class In Salesforce Is Not Difficult At All

Send mass email in Salesforce using apex code is easy now.


I have uploaded multiple contact Using data loader. I got one requirement to send an email with body to all contacts which are inserted.
Obviously we can add logic into Contact trigger on After Insert event to send mail.
In this blog, I am going to tell you about how to send mass emails from apex class.
I would like to share the approach which I used.

In my last blog, I have covered 3 Easy Steps To Send Emails With Attachment From Your Apex Class In Salesforce.

Below is the code snippet which sends an email to single user from apex. I have created one visual force page and controller associated with it. Visualforce page have Send Email button. 
Once user clicks on the button, I am sending email using apex code. 


Try this for sending emails to all contacts in my Salesforce org.

Step 1:

Create Apex Class with name DemoSendMassEmail and Paste below code snippet.

public class DemoSendMassEmail
{
     public void SendEmail()
     {
        String subject = 'Testing Mass email demo';
        String body = 'This is testing for Send mass emails from your apex class in Salesforce';       
        // Create a master list to store the emails that we will send
        List<Messaging.SingleEmailMessage> masterListMails =  
        new List<Messaging.SingleEmailMessage>();
        for (Contact myContact : [SELECT ID, Email FROM Contact])
        {
          // Define the email
          Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
           // Set list of peoples who should recieve an email
           List<String> sendTo = new List<String>();
           sendTo.add(myContact.Email);
           email.setToAddresses(sendTo);

            email.setSenderDisplayName('Official Nitish Account');

            // Sets the paramaters of the email
            email.setSubject( subject );
            email.setPlainTextBody( body );

            // Add your email address into the master list
             masterListMails.add(email);
         }
       // Sends mass emails
       Messaging.sendEmail(masterListMails);
      }
}

Step 2:

Create a VF page named as DemoSendMassEmail and paste the code snippet below.

<apex:page Controller="DemoSendMassEmail">
    <apex:form>
         <apex:commandButton value="Send Email!" action="{!SendEmail}"/>
     </apex:form>
</apex:page>

Step 3:

You can click on Preview button which is on Visualforce page 
OR 
You can Go to the URL and type (Change the yoursalesforceinstance with your salesforce org URL)


Click on Send Email button













3 Easy steps and we are done !!!


Make sure all contacts have valid email address else email will bounced back.

Before implementing mass emailSalesforceorce, you have to consider Email limits.


Email Limits in Salesforce:

  • Based on Greenwich Mean Time (GMT), you can send mass email up to a maximum of 5,000 external email addresses per day per org.
  • The single, as well as mass email limits, do not count unique addresses as one into account. For example, if you have nitishtest@example.com in your email 10 times, that counts as 10 against to the limit.
  • You can send as many amounts of email you want to your org’s internal users including portal users.
  • You can send mass email messages only to contacts, person accounts, leads, as well as your org’s internal users.
  • In Developer Edition orgs and orgs evaluating Salesforce during a trial period, you can send maximum 10 mass email to external email addresses per day.
Enjoy! If you have any questions, comments, please feel free to let me know.
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.


loading...

6 comments:

  1. Best information. Thanks for sharing such great information. hope you keep sharing such kind of information Bulk Mailer Pro

    ReplyDelete
  2. Thanks for sharing such a nice blog. Web email grabber help you to grab the genuine list of emails from websites email extractor

    ReplyDelete
  3. Thanks for sharing this news with us ! Nice blog !
    Bulk Email Software

    ReplyDelete
  4. Nice information thank you,if you want more information please visit our link salesforce certification

    ReplyDelete
  5. You can find perfect designs and backgrounds for your medical PowerPoint templates slide show, as we provide complete templates that combine top-quality graphics that make it easy to fill in the details.

    ReplyDelete
  6. You can find perfect designs and backgrounds for your medical PowerPoint templates slide show, as we provide complete templates that combine top-quality graphics that make it easy to fill in the details.

    ReplyDelete