Send free bulk emails by using Twilio SendGrid email API
Are you struggling when sending emails from your Java application? Are you using a SMTP server that is really slow? Then this article is for you. In this article, I’m going to show how we can use the Twilio SendGrid API to send bulk emails(a huge number of emails to several recipients) using Java. At the time of writing this article, the free plan of SendGrid provides a really good service. One free SendGrid account can send 100 emails per day forever.
It is really easy and efficient to send bulk emails using Twilio SendGrid API. We don’t need to install local SMTP servers and maintain them. Also, SendGrid API is much more efficient than local SMTP servers. Now let's see how to send an email using the Twilio SendGrid API.
Steps To Follow
- Create a SendGrid Account and get the account verified by clicking on the link they send you to your signed up email address.
2. Now you need to add and verify the email address that you are going to use to send emails. The verification link will come to the respective email address you added. This is a mandatory step to follow, or else you will be getting an error.
3. Now you need to create an API key with respective permissions. Once that is done you need to copy the API key. Go to settings from the left panel and create your API key.
At this point, your SendGrid account is ready for the job. Let’s now see how to implement and send an API request to SendGrid.
4. In your Java project you need to add the following dependencies. You can use Maven or Gradle build tools.
5. Now add the following code to your project in order to send bulk emails from your application. Replace the labels(<>) with necessary data. NOTE:- <SENDER’S_EMAIL_ADDRESS> should be validated(step 2) before doing this.
You can copy your API Key and paste it on the label <YOUR_API_KEY>.
Otherwise, you can copy your API Key and add it to your environment and retrieve the key from the environment.
Adding the API Key to the environment
export SENDGRID_API_KEY=<YOUR_API_KEY>
Retrieving the added API Key from the environment
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Now you can run the java application and test out the implementation. Also, you can see how many requests you got to the SendGrid API from its dashboard.
I hope now you have an idea of how to send emails from the SendGrid API.
Stay Safe and Happy Coding 🍻!