Sendgrid email tutorials in java with examples

Sendgrid is a SASS company, provides a transactional email provider and It is hosted on the cloud and enables integration in any application to send emails.

In this tutorial, You learned how to create a java project and add SendGrid dependencies and send emails using SendGrid with example

Create Java project using maven

Maven has different archetypes for different applications generated for web and standard java projects.

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

It creates a java sample module project.

Next add sendgrid maven jar dependency in pom.xml as seen below


Next, install the dependencies using the mvn clean install command. This installs dependencies for a project from the remote maven repository and downloads them to the local repository.

Send email using sendgrid java code example

In this example, Here are steps to configure SendGrid API in java code.

  • Create Email object for from and to fields
  • Initialize Content object with content type(text/Html) and HTML content.
  • Create com.sendgrid.helpers.mail.Mail object with from, to, subject, and content fields
  • Create SendGrid object using API key from SendGrid dashboard
  • Create request object with Method(Method.POST) and endPoint=“mail/send” and body
  • Call SendGrid API method request object which makes HTTP API call
  • Sendgrid system sends an email to recipients configured.
  • You can check your email status using the Response object.

Here is an code for Sendgrid API for sending email in java example

import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;


public class SendEmail {
    final private String sendGridApi = "";

    public static void main(String[] args) throws IOException {

            Email from = new Email("");
            Email to = new Email(""); // use your own email address here

            String subject = "This is test subject";
            Content content = new Content("text/html", "<strong>Test Content</strong>");

            Mail mail = new Mail(from, subject, to, content);

            SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
            Request request = new Request();


            Response response = sg.api(request);




You learned how to create a java application, add SendGrid maven dependency and java code for sending an email using java programming.