Skip to content

Commit bcea895

Browse files
authored
Update SendGrid Sample due to library changes (#1259)
* update sample with new library * linting * Google licenses updated * updated shared-configuration * update correct pom.xml
1 parent 806436b commit bcea895

File tree

5 files changed

+90
-37
lines changed

5 files changed

+90
-37
lines changed

appengine-java8/sendgrid/README.md

+35-12
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,56 @@
33
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java8/sendgrid/README.md">
44
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
55

6-
This sample demonstrates how to use [SendGrid](https://www.sendgrid.com) on
6+
This sample demonstrates how to use [SendGrid](http://sendgrid.com/partner/google) on
77
[Google App Engine standard environment][ae-docs].
88

99
See the [sample application documentaion][sample-docs] for more detailed
1010
instructions.
1111

1212
For more information about SendGrid, see their
13-
[documentation](https://sendgrid.com/docs/User_Guide/index.html).
13+
[documentation](https://sendgrid.com/docs/for-developers/).
1414

15-
[ae-docs]: https://cloud.google.com/appengine/docs/java/
15+
[ae-docs]: https://cloud.google.com/appengine/docs/standard/java/
1616
[sample-docs]: https://cloud.google.com/appengine/docs/java/mail/sendgrid
1717

18-
## Setup and deploy
18+
## Setup
1919

2020
Before you can run or deploy the sample, you will need to do the following:
2121

22-
1. [Create a SendGrid Account](http://sendgrid.com/partner/google). As of
23-
September 2015, Google users start with 25,000 free emails per month.
24-
1. Configure your SendGrid settings in the environment variables section in
25-
[`src/main/webapp/WEB-INF/appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml).
26-
1. Visit /send/email?to=YOUR-EMAIL-ADDRESS
22+
1. [Sign up with SendGrid via the GCP Console](https://console.cloud.google.com/launcher/details/sendgrid-app/sendgrid-email)
23+
and as a Google Cloud Platform developer, you can start with 12,000 free
24+
emails per month.
25+
1. Create an API key in SendGrid and configure your SendGrid settings in the
26+
environment variables section in [`appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml).
27+
28+
```XML
29+
<env-variables>
30+
<env-var name="SENDGRID_API_KEY" value="YOUR-SENDGRID-API-KEY" />
31+
<env-var name="SENDGRID_SENDER" value="YOUR-SENDGRID-SENDER" />
32+
</env-variables>
33+
```
2734

2835
## Running locally
2936

3037
You can run the application locally and send emails from your local machine. You
3138
will need to set environment variables before starting your application:
3239

33-
export SENDGRID_API_KEY=[your-sendgrid-api-key]
34-
export SENDGRID_SENDER=[your-sendgrid-sender-email-address]
35-
mvn clean appengine:run
40+
```shell
41+
export SENDGRID_API_KEY=[your-sendgrid-api-key]
42+
export SENDGRID_SENDER=[your-sendgrid-sender-email-address]
43+
mvn clean jetty:run
44+
```
45+
46+
To send an email, visit `localhost:8080/send/email?to=[EMAIL-ADDRESS]`
47+
in a web browser.
48+
49+
## Deploy
50+
51+
Deploy your application to App Engine standard with the following command:
52+
53+
```shell
54+
mvn appengine:deploy
55+
```
56+
57+
To send an email, visit `https://[YOUR-PROJECT-ID].appspot.com/send/email?to=[EMAIL-ADDRESS]`
58+
in a web browser.

appengine-java8/sendgrid/pom.xml

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
Copyright 2015 Google Inc.
2+
Copyright 2018 Google LLC
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@ Copyright 2015 Google Inc.
2727
<parent>
2828
<groupId>com.google.cloud.samples</groupId>
2929
<artifactId>shared-configuration</artifactId>
30-
<version>1.0.9</version>
30+
<version>1.0.10</version>
3131
</parent>
3232

3333
<properties>
@@ -44,13 +44,13 @@ Copyright 2015 Google Inc.
4444
<scope>provided</scope>
4545
</dependency>
4646

47-
<!-- [START dependencies] -->
47+
<!-- [START gae_sendgrid_dependencies] -->
4848
<dependency>
4949
<groupId>com.sendgrid</groupId>
5050
<artifactId>sendgrid-java</artifactId>
51-
<version>2.2.2</version>
51+
<version>4.3.0</version>
5252
</dependency>
53-
<!-- [END dependencies] -->
53+
<!-- [END gae_sendgrid_dependencies] -->
5454
</dependencies>
5555
<build>
5656
<!-- for hot reload of the web application -->
@@ -59,12 +59,17 @@ Copyright 2015 Google Inc.
5959
<plugin>
6060
<groupId>com.google.cloud.tools</groupId>
6161
<artifactId>appengine-maven-plugin</artifactId>
62-
<version>1.3.1</version>
62+
<version>1.3.2</version>
6363
<configuration>
6464
<deploy.promote>true</deploy.promote>
6565
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
6666
</configuration>
6767
</plugin>
68+
<plugin>
69+
<groupId>org.eclipse.jetty</groupId>
70+
<artifactId>jetty-maven-plugin</artifactId>
71+
<version>9.4.12.v20180830</version>
72+
</plugin>
6873
</plugins>
6974
</build>
7075
</project>

appengine-java8/sendgrid/src/main/java/com/example/appengine/sendgrid/SendEmailServlet.java

+40-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 Google Inc.
2+
* Copyright 2018 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,47 +16,72 @@
1616

1717
package com.example.appengine.sendgrid;
1818

19+
// [START gae_sendgrid_import]
20+
import com.sendgrid.Content;
21+
import com.sendgrid.Email;
22+
import com.sendgrid.Mail;
23+
import com.sendgrid.Method;
24+
import com.sendgrid.Request;
25+
import com.sendgrid.Response;
1926
import com.sendgrid.SendGrid;
20-
import com.sendgrid.SendGridException;
2127
import java.io.IOException;
2228
import javax.servlet.ServletException;
2329
import javax.servlet.http.HttpServlet;
2430
import javax.servlet.http.HttpServletRequest;
2531
import javax.servlet.http.HttpServletResponse;
32+
// [END gae_sendgrid_import]
2633

27-
// [START example]
2834
@SuppressWarnings("serial")
2935
public class SendEmailServlet extends HttpServlet {
3036

3137
@Override
32-
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException,
33-
ServletException {
38+
public void service(HttpServletRequest req, HttpServletResponse resp)
39+
throws IOException, ServletException {
40+
// Get parameters from environment variables.
3441
final String sendgridApiKey = System.getenv("SENDGRID_API_KEY");
3542
final String sendgridSender = System.getenv("SENDGRID_SENDER");
43+
44+
// Get email from query string.
3645
final String toEmail = req.getParameter("to");
3746
if (toEmail == null) {
3847
resp.getWriter()
3948
.print("Please provide an email address in the \"to\" query string parameter.");
4049
return;
4150
}
4251

52+
// [START gae_sendgrid]
53+
// Set content for request.
54+
Email to = new Email(toEmail);
55+
Email from = new Email(sendgridSender);
56+
String subject = "This is a test email";
57+
Content content = new Content("text/plain", "Example text body.");
58+
Mail mail = new Mail(from, subject, to, content);
59+
60+
// Instantiates SendGrid client.
4361
SendGrid sendgrid = new SendGrid(sendgridApiKey);
44-
SendGrid.Email email = new SendGrid.Email();
45-
email.addTo(toEmail);
46-
email.setFrom(sendgridSender);
47-
email.setSubject("This is a test email");
48-
email.setText("Example text body.");
62+
63+
// Instantiate SendGrid request.
64+
Request request = new Request();
4965

5066
try {
51-
SendGrid.Response response = sendgrid.send(email);
52-
if (response.getCode() != 200) {
53-
resp.getWriter().print(String.format("An error occured: %s", response.getMessage()));
67+
// Set request configuration.
68+
request.setMethod(Method.POST);
69+
request.setEndpoint("mail/send");
70+
request.setBody(mail.build());
71+
72+
// Use the client to send the API request.
73+
Response response = sendgrid.api(request);
74+
75+
if (response.getStatusCode() != 202) {
76+
resp.getWriter().print(String.format("An error occured: %s", response.getStatusCode()));
5477
return;
5578
}
79+
80+
// Print response.
5681
resp.getWriter().print("Email sent.");
57-
} catch (SendGridException e) {
82+
} catch (IOException e) {
5883
throw new ServletException("SendGrid error", e);
5984
}
85+
// [END gae_sendgrid]
6086
}
6187
}
62-
// [END example]

appengine-java8/sendgrid/src/main/webapp/WEB-INF/appengine-web.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- [START_EXCLUDE] -->
33
<!--
4-
Copyright 2015 Google Inc.
4+
Copyright 2018 Google LLC
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
77
You may obtain a copy of the License at
@@ -16,9 +16,10 @@
1616
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
1717
<runtime>java8</runtime>
1818
<threadsafe>true</threadsafe>
19+
<!-- [START gae_sendgrid_config] -->
1920
<env-variables>
2021
<env-var name="SENDGRID_API_KEY" value="YOUR-SENDGRID-API-KEY" />
2122
<env-var name="SENDGRID_SENDER" value="YOUR-SENDGRID-SENDER" />
2223
</env-variables>
24+
<!-- [END gae_sendgrid_config] -->
2325
</appengine-web-app>
24-

appengine-java8/sendgrid/src/main/webapp/WEB-INF/web.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!--
3-
Copyright 2017 Google Inc.
3+
Copyright 2018 Google LLC
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -28,4 +28,3 @@
2828
<url-pattern>/send/email</url-pattern>
2929
</servlet-mapping>
3030
</web-app>
31-

0 commit comments

Comments
 (0)