Skip to content

Commit 165af88

Browse files
author
Shun Fan
committed
Add compute sendgrid example
1 parent 735a4cf commit 165af88

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

compute/sendgrid/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Java SendGrid Email Sample for Google Compute Engine
2+
3+
This sample demonstrates how to use [SendGrid](https://www.sendgrid.com) on
4+
[Google Compute Engine](https://cloud.google.com/compute/)
5+
6+
See the [sample application documentaion][sample-docs] for more detailed
7+
instructions.
8+
9+
For more information about SendGrid, see their
10+
[documentation](https://sendgrid.com/docs/User_Guide/index.html).
11+
12+
[sample-docs]: https://cloud.google.com/compute/docs/tutorials/sending-mail/using-sendgrid
13+
14+
## Setup
15+
16+
Before you can run or deploy the sample, you will need to do the following:
17+
18+
1. [Create a SendGrid Account](http://sendgrid.com/partner/google). As of
19+
September 2015, Google users start with 25,000 free emails per month.
20+
1. Create a compute instance on the Google Cloud Platform Developer's Console
21+
1. SSH into that instance. If SSHing from the Developer's Console, switch to user managed
22+
if necessary.
23+
1. Update packages and install required packages
24+
sudo apt-get update && sudo apt-get install git-core openjdk-8-jdk maven
25+
1. Clone the repo
26+
git clone https://github.com/shun-fan/test-compute-sendgrid.git
27+
1. Configure your SendGrid settings in the java class (SENDGRID_API_KEY, SENDGRID_SENDER, TO_EMAIL)
28+
./sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java
29+
1. Navigate back to ./sendgrid and use maven to package the class as a jar
30+
mvn clean package
31+
1. Switch to the target directory with the jar file and enable execution on that file
32+
chmod +x compute-sendgrid-1.0-SNAPSHOT-jar-with-dependencies.jar
33+
1. Make sure that openjdk 8 is the selected java version
34+
sudo update-alternatives --config java
35+
1. Execute the jar file and send an email
36+
java -jar compute-sendgrid-1.0-SNAPSHOT-jar-with-dependencies.jar
37+

compute/sendgrid/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!-
2+
Copyright 2016 Google Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>jar</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.compute</groupId>
21+
<artifactId>compute-sendgrid</artifactId>
22+
23+
<dependencies>
24+
<!-- [START dependencies] -->
25+
<dependency>
26+
<groupId>com.sendgrid</groupId>
27+
<artifactId>sendgrid-java</artifactId>
28+
<version>2.2.2</version>
29+
</dependency>
30+
<!-- [END dependencies] -->
31+
</dependencies>
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<artifactId>maven-assembly-plugin</artifactId>
36+
<executions>
37+
<execution>
38+
<phase>package</phase>
39+
<goals>
40+
<goal>single</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
<configuration>
45+
<archive>
46+
<manifest>
47+
<mainClass>com.example.compute.sendgrid.SendEmailServlet</mainClass>
48+
</manifest>
49+
</archive>
50+
<descriptorRefs>
51+
<descriptorRef>jar-with-dependencies</descriptorRef>
52+
</descriptorRefs>
53+
</configuration>
54+
</plugin>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<version>3.3</version>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<configuration>
60+
<source>1.8</source>
61+
<target>1.8</target>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compute.sendgrid;
18+
19+
import com.sendgrid.SendGrid;
20+
import com.sendgrid.SendGridException;
21+
22+
// [START example]
23+
public class SendEmailServlet {
24+
final static String SENDGRID_API_KEY = "YOUR-SENDGRID-API-KEY";
25+
final static String SENDGRID_SENDER = "YOUR-SENDGRID-SENDER";
26+
final static String TO_EMAIL = "DESTINATION-EMAIL";
27+
28+
public static void main(String[] args) throws SendGridException {
29+
30+
SendGrid sendgrid = new SendGrid(SENDGRID_API_KEY);
31+
SendGrid.Email email = new SendGrid.Email();
32+
email.addTo(TO_EMAIL);
33+
email.setFrom(SENDGRID_SENDER);
34+
email.setSubject("This is a test email");
35+
email.setText("Example text body.");
36+
37+
SendGrid.Response response = sendgrid.send(email);
38+
if (response.getCode() != 200) {
39+
System.out.print(String.format("An error occured: %s", response.getMessage()));
40+
return;
41+
}
42+
System.out.print("Email sent.");
43+
}
44+
45+
}
46+
// [END example]

0 commit comments

Comments
 (0)