Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compute sendgrid example #163

Merged
merged 3 commits into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions compute/sendgrid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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

## Setup

Before you can run or deploy 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 that instance. If SSHing from the Developer's Console, switch to user managed
if necessary.
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
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-SENDER";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the value for sendgrid-sender an email address? Maybe something like "SENDER-EMAIL" to be consistent with "DESTINATION-EMAIL"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

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]