From 165af882dbf454b5f8c91d06e543569c0dfa580a Mon Sep 17 00:00:00 2001 From: Shun Fan Date: Fri, 15 Apr 2016 14:06:39 -0700 Subject: [PATCH 1/3] Add compute sendgrid example --- compute/sendgrid/README.md | 37 +++++++++++ compute/sendgrid/pom.xml | 66 +++++++++++++++++++ .../compute/sendgrid/SendEmailServlet.java | 46 +++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 compute/sendgrid/README.md create mode 100644 compute/sendgrid/pom.xml create mode 100644 compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java diff --git a/compute/sendgrid/README.md b/compute/sendgrid/README.md new file mode 100644 index 00000000000..952b91b7186 --- /dev/null +++ b/compute/sendgrid/README.md @@ -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 + diff --git a/compute/sendgrid/pom.xml b/compute/sendgrid/pom.xml new file mode 100644 index 00000000000..b136a1c28e3 --- /dev/null +++ b/compute/sendgrid/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + jar + 1.0-SNAPSHOT + com.example.compute + compute-sendgrid + + + + + com.sendgrid + sendgrid-java + 2.2.2 + + + + + + + maven-assembly-plugin + + + package + + single + + + + + + + com.example.compute.sendgrid.SendEmailServlet + + + + jar-with-dependencies + + + + + org.apache.maven.plugins + 3.3 + maven-compiler-plugin + + 1.8 + 1.8 + + + + + diff --git a/compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java b/compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java new file mode 100644 index 00000000000..e7d1a363019 --- /dev/null +++ b/compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java @@ -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"; + 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] From 3275972b1da344a8e220231a7e1ed3f3f1be52cc Mon Sep 17 00:00:00 2001 From: Shun Fan Date: Fri, 15 Apr 2016 14:36:48 -0700 Subject: [PATCH 2/3] Update readme on compute sendgrid --- compute/sendgrid/README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/compute/sendgrid/README.md b/compute/sendgrid/README.md index 952b91b7186..e1dc94c7119 100644 --- a/compute/sendgrid/README.md +++ b/compute/sendgrid/README.md @@ -11,15 +11,14 @@ For more information about SendGrid, see their [sample-docs]: https://cloud.google.com/compute/docs/tutorials/sending-mail/using-sendgrid -## Setup +## Running on Compute Engine -Before you can run or deploy the sample, you will need to do the following: +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 that instance. If SSHing from the Developer's Console, switch to user managed - if necessary. +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 @@ -32,6 +31,6 @@ Before you can run or deploy the sample, you will need to do the following: 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 +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 From 4ba18b4934506f0939cbf34ba76ec9215c759c02 Mon Sep 17 00:00:00 2001 From: Shun Fan Date: Mon, 18 Apr 2016 13:52:41 -0700 Subject: [PATCH 3/3] Update sendgrid string --- .../java/com/example/compute/sendgrid/SendEmailServlet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java b/compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java index e7d1a363019..6c98fd8d8cb 100644 --- a/compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java +++ b/compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java @@ -22,7 +22,7 @@ // [START example] public class SendEmailServlet { final static String SENDGRID_API_KEY = "YOUR-SENDGRID-API-KEY"; - final static String SENDGRID_SENDER = "YOUR-SENDGRID-SENDER"; + final static String SENDGRID_SENDER = "YOUR-SENDGRID-FROM-EMAIL"; final static String TO_EMAIL = "DESTINATION-EMAIL"; public static void main(String[] args) throws SendGridException {