Skip to content

Commit

Permalink
Merge pull request #163 from GoogleCloudPlatform/compute-sendgrid
Browse files Browse the repository at this point in the history
Add compute sendgrid example
  • Loading branch information
Shun Fan committed Apr 18, 2016
2 parents 21dc731 + 4ba18b4 commit f394e51
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
36 changes: 36 additions & 0 deletions compute/sendgrid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Java SendGrid Email Sample for Google Compute Engine

This sample demonstrates how to use [SendGrid](https://www.sendgrid.com) on
[Google Compute Engine](https://cloud.google.com/compute/)

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

For more information about SendGrid, see their
[documentation](https://sendgrid.com/docs/User_Guide/index.html).

[sample-docs]: https://cloud.google.com/compute/docs/tutorials/sending-mail/using-sendgrid

## Running on Compute Engine

To run the sample, you will need to do the following:

1. [Create a SendGrid Account](http://sendgrid.com/partner/google). As of
September 2015, Google users start with 25,000 free emails per month.
1. Create a compute instance on the Google Cloud Platform Developer's Console
1. SSH into the instance you created
1. Update packages and install required packages
sudo apt-get update && sudo apt-get install git-core openjdk-8-jdk maven
1. Clone the repo
git clone https://github.com/shun-fan/test-compute-sendgrid.git
1. Configure your SendGrid settings in the java class (SENDGRID_API_KEY, SENDGRID_SENDER, TO_EMAIL)
./sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java
1. Navigate back to ./sendgrid and use maven to package the class as a jar
mvn clean package
1. Switch to the target directory with the jar file and enable execution on that file
chmod +x compute-sendgrid-1.0-SNAPSHOT-jar-with-dependencies.jar
1. Make sure that openjdk 8 is the selected java version
sudo update-alternatives --config java
1. Execute the jar file and send an email (make sure you are in the target folder)
java -jar compute-sendgrid-1.0-SNAPSHOT-jar-with-dependencies.jar

66 changes: 66 additions & 0 deletions compute/sendgrid/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!-
Copyright 2016 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.compute</groupId>
<artifactId>compute-sendgrid</artifactId>

<dependencies>
<!-- [START dependencies] -->
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>2.2.2</version>
</dependency>
<!-- [END dependencies] -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.example.compute.sendgrid.SendEmailServlet</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.3</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.compute.sendgrid;

import com.sendgrid.SendGrid;
import com.sendgrid.SendGridException;

// [START example]
public class SendEmailServlet {
final static String SENDGRID_API_KEY = "YOUR-SENDGRID-API-KEY";
final static String SENDGRID_SENDER = "YOUR-SENDGRID-FROM-EMAIL";
final static String TO_EMAIL = "DESTINATION-EMAIL";

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

SendGrid sendgrid = new SendGrid(SENDGRID_API_KEY);
SendGrid.Email email = new SendGrid.Email();
email.addTo(TO_EMAIL);
email.setFrom(SENDGRID_SENDER);
email.setSubject("This is a test email");
email.setText("Example text body.");

SendGrid.Response response = sendgrid.send(email);
if (response.getCode() != 200) {
System.out.print(String.format("An error occured: %s", response.getMessage()));
return;
}
System.out.print("Email sent.");
}

}
// [END example]

0 comments on commit f394e51

Please sign in to comment.