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

    <dependency>
      <groupId>com.sendgrid</groupId>
      <artifactId>sendgrid-java</artifactId>
      <version>4.7.2</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.30</version>
      <scope>test</scope>
    </dependency>

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;

import java.io.IOException;

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();

            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());

            Response response = sg.api(request);

            System.out.println(response.getStatusCode());
            System.out.println(response.getHeaders());
            System.out.println(response.getBody());
        }

    }

Conclusion

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