-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
46 changes: 46 additions & 0 deletions
46
compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
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] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1