-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from GoogleCloudPlatform/add-mailjet
Add compute mailjet sample
- Loading branch information
Showing
20 changed files
with
275 additions
and
36 deletions.
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,44 @@ | ||
# Java Mailjet Email Sample for Google Compute Engine | ||
|
||
This sample demonstrates how to use [Mailjet](https://www.mailjet.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 Mailjet, see their | ||
[documentation](http://dev.mailjet.com/email-api/v3/apikey/). | ||
|
||
[sample-docs]: https://cloud.google.com/compute/docs/tutorials/sending-mail/ | ||
|
||
## Running on Compute Engine | ||
|
||
To run the sample, you will need to do the following: | ||
|
||
1. [Create a Mailjet Account](https://app.mailjet.com/signup). | ||
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/GoogleCloudPlatform/java-docs-samples.git` | ||
|
||
1. Configure your Mailjet settings in the java class (MAILJET_API_KEY, SENDGRID_SENDER) | ||
|
||
`java-docs-samples/compute/mailjet/src/main/java/com/example/compute/mailjet/MailjetSender.java` | ||
|
||
1. Navigate back to ./Mailjet and use maven to package the class as a jar | ||
|
||
`mvn clean package` | ||
|
||
1. Make sure that openjdk 8 is the selected java version | ||
|
||
`sudo update-alternatives --config java` | ||
|
||
1. Execute the jar file with your intended recipient and sender emails as arguments | ||
and send an email (make sure you are in the target folder) | ||
|
||
`java -jar compute-mailjet-1.0-SNAPSHOT-jar-with-dependencies.jar [RECIPIENT EMAIL] [SENDER EMAIL]` |
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,91 @@ | ||
<!-- | ||
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-mailjet</artifactId> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
<!-- [START dependencies] --> | ||
<dependency> | ||
<groupId>com.mailjet</groupId> | ||
<artifactId>mailjet-client</artifactId> | ||
<version>4.0.1</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.mailjet.MailjetSender</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> |
66 changes: 66 additions & 0 deletions
66
compute/mailjet/src/main/java/com/example/compute/mailjet/MailjetSender.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,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. | ||
*/ | ||
|
||
package com.example.compute.mailjet; | ||
|
||
// [START mailjet_imports] | ||
import com.mailjet.client.MailjetClient; | ||
import com.mailjet.client.MailjetRequest; | ||
import com.mailjet.client.MailjetResponse; | ||
import com.mailjet.client.errors.MailjetException; | ||
import com.mailjet.client.resource.Email; | ||
// [END mailjet_imports] | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
// [START app] | ||
public class MailjetSender{ | ||
|
||
public static void main(String[] args) { | ||
final String mailjetApiKey = "YOUR-MAILJET-API-KEY"; | ||
final String mailjetSecretKey = "YOUR-MAILJET-SECRET-KEY"; | ||
MailjetClient client = new MailjetClient(mailjetApiKey, mailjetSecretKey); | ||
|
||
MailjetSender sender = new MailjetSender(); | ||
sender.sendMailjet(args[0], args[1], client); | ||
} | ||
|
||
public MailjetResponse sendMailjet(String recipient, String sender, MailjetClient client) { | ||
MailjetRequest email = new MailjetRequest(Email.resource) | ||
.property(Email.FROMEMAIL, sender) | ||
.property(Email.FROMNAME, "pandora") | ||
.property(Email.SUBJECT, "Your email flight plan!") | ||
.property(Email.TEXTPART, | ||
"Dear passenger, welcome to Mailjet! May the delivery force be with you!") | ||
.property(Email.HTMLPART, | ||
"<h3>Dear passenger, welcome to Mailjet!</h3><br/>May the delivery force be with you!") | ||
.property(Email.RECIPIENTS, new JSONArray().put(new JSONObject().put("Email", recipient))); | ||
|
||
try { | ||
// trigger the API call | ||
MailjetResponse response = client.post(email); | ||
// Read the response data and status | ||
System.out.println(response.getStatus()); | ||
System.out.println(response.getData()); | ||
return response; | ||
} catch (MailjetException e) { | ||
System.out.println("Mailjet Exception: " + e); | ||
return null; | ||
} | ||
} | ||
} | ||
// [END app] |
54 changes: 54 additions & 0 deletions
54
compute/mailjet/src/test/java/com/example/compute/mailjet/MailjetSenderTest.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,54 @@ | ||
/** | ||
* 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.mailjet; | ||
|
||
import com.mailjet.client.MailjetClient; | ||
import com.mailjet.client.MailjetResponse; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mockito.Matchers; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
/** | ||
* Unit tests for {@link MailjetSender}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class MailjetSenderTest { | ||
|
||
@Mock MailjetClient mockClient; | ||
@Mock MailjetResponse mockResponse; | ||
private MailjetSender sender; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
|
||
Mockito.when(mockClient.post(Matchers.anyObject())).thenReturn(mockResponse); | ||
sender = new MailjetSender(); | ||
} | ||
|
||
@Test | ||
public void doGet_defaultEnvironment_writesResponse() throws Exception { | ||
sender.sendMailjet("fake recipient", "fake sender", mockClient); | ||
Mockito.verify(mockClient).post(Matchers.anyObject()); | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.