Sending Email with SendGrid

You can use SendGrid to power your emails on Google App Engine. Using SendGrid can improve your deliverability and provide transparency into what actually happens to those emails your app sends. You can see statistics on opens, clicks, unsubscribes, spam reports and more through either the SendGrid interface or its API.


Google App Engine customers can send 12,000 emails every month for free. Follow the steps below to sign up for the free emails and to see higher volume plans.

Before you begin

To begin, you'll need to:

  1. Create or select a GCP project in the GCP Console and then ensure that project includes an App Engine application:

    Go to App Engine

    The Dashboard opens if an App Engine application already exists in your project. Otherwise, you are prompted to choose the region where you want your App Engine application located.

  2. Sign up with SendGrid via the GCP Console and as a Google Cloud Platform developer, you can start with 12,000 free emails per month.

  3. To deploy a Java app to App Engine, you must first setup your environment, see Deploying a Java App for details.

    Download the SDK

Sending an email

With the prerequisites complete, make sure you are set up for Java on your local machine.

Add the required Java library for SendGrid modules to your application. For example, if you use Maven, add to the project's pom.xml:


Create an API key in SendGrid and configure the domain and the API key. You can either use the domain you've configured with SendGrid or their test domain.

  <env-var name="SENDGRID_API_KEY" value="YOUR-SENDGRID-API-KEY" />
  <env-var name="SENDGRID_SENDER" value="YOUR-SENDGRID-SENDER" />

Import the SendGrid library in your application code:

import com.sendgrid.Content;
import com.sendgrid.Email;
import com.sendgrid.Mail;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Now, from within your app, you can send email with the following lines:

// Set content for request.
Email to = new Email(toEmail);
Email from = new Email(sendgridSender);
String subject = "This is a test email";
Content content = new Content("text/plain", "Example text body.");
Mail mail = new Mail(from, subject, to, content);

// Instantiates SendGrid client.
SendGrid sendgrid = new SendGrid(sendgridApiKey);

// Instantiate SendGrid request.
Request request = new Request();

try {
  // Set request configuration.

  // Use the client to send the API request.
  Response response = sendgrid.api(request);

  if (response.getStatusCode() != 202) {
    resp.getWriter().print(String.format("An error occurred: %s", response.getStatusCode()));

  // Print response.
  resp.getWriter().print("Email sent.");
} catch (IOException e) {
  throw new ServletException("SendGrid error", e);

You'll want to add your own account details. Then edit the email address and other message content.

When you are ready to deploy your app to App Engine, you can use either Maven (mvn appengine:deploy) or the appcfg command-line tool. For details, see Deploying Your App.

For more information about email in your apps, browse the SendGrid documentation.

Getting real-time information

In addition to sending email, SendGrid can help you receive email or make sense of the email you’ve already sent. The two real-time webhook solutions can greatly enhance the role email plays in your application.

Using the Event API

Once you start sending email from your app, you'll want to know more about how it's performing. The statistics within SendGrid are one of its best features. The Event API lets you see all this data as one giant firehose. Whenever a recipient opens or clicks an email, for example, SendGrid can send a small bit of descriptive JSON to your Google App Engine app. You can react to the event or store the data for future use.

There are many different applications of event data. Some common uses are to integrate email stats into internal dashboards or use it to respond immediately to unsubscribes and spam reports. Advanced users of the Event API raise the engagement of their emails by sending only to those who have clicked or opened within the last few months.

The Event API documentation shows how to set up the webhook, outlines the nine event types and shows the fields included in event callbacks.

Using the Inbound Parse API

You can also use SendGrid to receive email. The Inbound Parse API can be used for powerful, interactive applications. For example, automate support tickets from customer emails, or collect data via short emails employees dispatch from the road. NudgeMail's reminder application is even built entirely on the Parse API.

Like the Event API, the Parse API is a webhook that sends data to your application when something new is available. In this case, the webhook is called whenever a new email arrives at the domain you've associated with incoming email. Due to intricacies in the way DNS works for email, you need to assign all incoming mail to the domain or subdomain you use for the Parse API.

Emails are sent to your application structured as JSON, with sender, recipients, subject and body as different fields. You can even accept attachments, within SendGrid’s limit of messages up to 20 MB in size.

The Parse API documentation has more details, including additional fields sent with every email, as well as instructions for DNS setup and usage.

* Google will be compensated for customers who sign up for a non-free account.

Oliko tästä sivusta apua? Kerro mielipiteesi

Palautteen aihe:

Tämä sivu
App Engine standard environment for Java