Skip to content

Commit 94c3b29

Browse files
author
Shun Fan
committed
Merge pull request GoogleCloudPlatform#235 from GoogleCloudPlatform/add-mailjet
Add compute mailjet sample
2 parents cd1a3b3 + e6ac091 commit 94c3b29

File tree

20 files changed

+275
-36
lines changed

20 files changed

+275
-36
lines changed

compute/mailjet/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Java Mailjet Email Sample for Google Compute Engine
2+
3+
This sample demonstrates how to use [Mailjet](https://www.mailjet.com/) on
4+
[Google Compute Engine](https://cloud.google.com/compute/)
5+
6+
See the [sample application documentaion][sample-docs] for more detailed
7+
instructions.
8+
9+
For more information about Mailjet, see their
10+
[documentation](http://dev.mailjet.com/email-api/v3/apikey/).
11+
12+
[sample-docs]: https://cloud.google.com/compute/docs/tutorials/sending-mail/
13+
14+
## Running on Compute Engine
15+
16+
To run the sample, you will need to do the following:
17+
18+
1. [Create a Mailjet Account](https://app.mailjet.com/signup).
19+
1. Create a compute instance on the Google Cloud Platform Developer's Console
20+
1. SSH into the instance you created
21+
1. Update packages and install required packages
22+
23+
`sudo apt-get update && sudo apt-get install git-core openjdk-8-jdk maven`
24+
25+
1. Clone the repo
26+
27+
`git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git`
28+
29+
1. Configure your Mailjet settings in the java class (MAILJET_API_KEY, SENDGRID_SENDER)
30+
31+
`java-docs-samples/compute/mailjet/src/main/java/com/example/compute/mailjet/MailjetSender.java`
32+
33+
1. Navigate back to ./Mailjet and use maven to package the class as a jar
34+
35+
`mvn clean package`
36+
37+
1. Make sure that openjdk 8 is the selected java version
38+
39+
`sudo update-alternatives --config java`
40+
41+
1. Execute the jar file with your intended recipient and sender emails as arguments
42+
and send an email (make sure you are in the target folder)
43+
44+
`java -jar compute-mailjet-1.0-SNAPSHOT-jar-with-dependencies.jar [RECIPIENT EMAIL] [SENDER EMAIL]`

compute/mailjet/pom.xml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<!--
2+
Copyright 2016 Google Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>jar</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.compute</groupId>
21+
<artifactId>compute-mailjet</artifactId>
22+
<parent>
23+
<groupId>com.google.cloud</groupId>
24+
<artifactId>doc-samples</artifactId>
25+
<version>1.0.0</version>
26+
<relativePath>../..</relativePath>
27+
</parent>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<version>4.10</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.mockito</groupId>
38+
<artifactId>mockito-all</artifactId>
39+
<version>1.10.19</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>javax.servlet-api</artifactId>
45+
<version>3.1.0</version>
46+
<type>jar</type>
47+
<scope>provided</scope>
48+
</dependency>
49+
<!-- [START dependencies] -->
50+
<dependency>
51+
<groupId>com.mailjet</groupId>
52+
<artifactId>mailjet-client</artifactId>
53+
<version>4.0.1</version>
54+
</dependency>
55+
<!-- [END dependencies] -->
56+
</dependencies>
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<artifactId>maven-assembly-plugin</artifactId>
61+
<executions>
62+
<execution>
63+
<phase>package</phase>
64+
<goals>
65+
<goal>single</goal>
66+
</goals>
67+
</execution>
68+
</executions>
69+
<configuration>
70+
<archive>
71+
<manifest>
72+
<mainClass>com.example.compute.mailjet.MailjetSender</mainClass>
73+
</manifest>
74+
</archive>
75+
<descriptorRefs>
76+
<descriptorRef>jar-with-dependencies</descriptorRef>
77+
</descriptorRefs>
78+
</configuration>
79+
</plugin>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<version>3.3</version>
83+
<artifactId>maven-compiler-plugin</artifactId>
84+
<configuration>
85+
<source>1.8</source>
86+
<target>1.8</target>
87+
</configuration>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compute.mailjet;
18+
19+
// [START mailjet_imports]
20+
import com.mailjet.client.MailjetClient;
21+
import com.mailjet.client.MailjetRequest;
22+
import com.mailjet.client.MailjetResponse;
23+
import com.mailjet.client.errors.MailjetException;
24+
import com.mailjet.client.resource.Email;
25+
// [END mailjet_imports]
26+
27+
import org.json.JSONArray;
28+
import org.json.JSONObject;
29+
30+
// [START app]
31+
public class MailjetSender{
32+
33+
public static void main(String[] args) {
34+
final String mailjetApiKey = "YOUR-MAILJET-API-KEY";
35+
final String mailjetSecretKey = "YOUR-MAILJET-SECRET-KEY";
36+
MailjetClient client = new MailjetClient(mailjetApiKey, mailjetSecretKey);
37+
38+
MailjetSender sender = new MailjetSender();
39+
sender.sendMailjet(args[0], args[1], client);
40+
}
41+
42+
public MailjetResponse sendMailjet(String recipient, String sender, MailjetClient client) {
43+
MailjetRequest email = new MailjetRequest(Email.resource)
44+
.property(Email.FROMEMAIL, sender)
45+
.property(Email.FROMNAME, "pandora")
46+
.property(Email.SUBJECT, "Your email flight plan!")
47+
.property(Email.TEXTPART,
48+
"Dear passenger, welcome to Mailjet! May the delivery force be with you!")
49+
.property(Email.HTMLPART,
50+
"<h3>Dear passenger, welcome to Mailjet!</h3><br/>May the delivery force be with you!")
51+
.property(Email.RECIPIENTS, new JSONArray().put(new JSONObject().put("Email", recipient)));
52+
53+
try {
54+
// trigger the API call
55+
MailjetResponse response = client.post(email);
56+
// Read the response data and status
57+
System.out.println(response.getStatus());
58+
System.out.println(response.getData());
59+
return response;
60+
} catch (MailjetException e) {
61+
System.out.println("Mailjet Exception: " + e);
62+
return null;
63+
}
64+
}
65+
}
66+
// [END app]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compute.mailjet;
18+
19+
import com.mailjet.client.MailjetClient;
20+
import com.mailjet.client.MailjetResponse;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
import org.mockito.Matchers;
27+
import org.mockito.Mock;
28+
import org.mockito.Mockito;
29+
import org.mockito.MockitoAnnotations;
30+
31+
/**
32+
* Unit tests for {@link MailjetSender}.
33+
*/
34+
@RunWith(JUnit4.class)
35+
public class MailjetSenderTest {
36+
37+
@Mock MailjetClient mockClient;
38+
@Mock MailjetResponse mockResponse;
39+
private MailjetSender sender;
40+
41+
@Before
42+
public void setUp() throws Exception {
43+
MockitoAnnotations.initMocks(this);
44+
45+
Mockito.when(mockClient.post(Matchers.anyObject())).thenReturn(mockResponse);
46+
sender = new MailjetSender();
47+
}
48+
49+
@Test
50+
public void doGet_defaultEnvironment_writesResponse() throws Exception {
51+
sender.sendMailjet("fake recipient", "fake sender", mockClient);
52+
Mockito.verify(mockClient).post(Matchers.anyObject());
53+
}
54+
}

managed_vms/analytics/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<plugin>
3636
<groupId>com.google.appengine</groupId>
3737
<artifactId>gcloud-maven-plugin</artifactId>
38-
<version>2.0.9.101.v20160316</version>
38+
<version>2.0.9.106.v20160420</version>
3939
</plugin>
4040
<plugin>
4141
<groupId>org.apache.maven.plugins</groupId>

managed_vms/async-rest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<plugin>
5454
<groupId>com.google.appengine</groupId>
5555
<artifactId>gcloud-maven-plugin</artifactId>
56-
<version>2.0.9.101.v20160316</version>
56+
<version>2.0.9.106.v20160420</version>
5757
<configuration>
5858
<verbosity>debug</verbosity>
5959
<log_level>debug</log_level>

managed_vms/cloudstorage/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<plugin>
3838
<groupId>com.google.appengine</groupId>
3939
<artifactId>gcloud-maven-plugin</artifactId>
40-
<version>2.0.9.101.v20160316</version>
40+
<version>2.0.9.106.v20160420</version>
4141
</plugin>
4242
<plugin>
4343
<groupId>org.apache.maven.plugins</groupId>

managed_vms/cron/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<plugin>
3535
<groupId>com.google.appengine</groupId>
3636
<artifactId>gcloud-maven-plugin</artifactId>
37-
<version>2.0.9.101.v20160316</version>
37+
<version>2.0.9.106.v20160420</version>
3838
</plugin>
3939
<plugin>
4040
<groupId>org.apache.maven.plugins</groupId>

managed_vms/datastore/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<plugin>
3838
<groupId>com.google.appengine</groupId>
3939
<artifactId>gcloud-maven-plugin</artifactId>
40-
<version>2.0.9.101.v20160316</version>
40+
<version>2.0.9.106.v20160420</version>
4141
</plugin>
4242
<plugin>
4343
<groupId>org.apache.maven.plugins</groupId>

managed_vms/disk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<plugin>
3131
<groupId>com.google.appengine</groupId>
3232
<artifactId>gcloud-maven-plugin</artifactId>
33-
<version>2.0.9.101.v20160316</version>
33+
<version>2.0.9.106.v20160420</version>
3434
</plugin>
3535
<plugin>
3636
<groupId>org.apache.maven.plugins</groupId>

managed_vms/extending-runtime/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<plugin>
3131
<groupId>com.google.appengine</groupId>
3232
<artifactId>gcloud-maven-plugin</artifactId>
33-
<version>2.0.9.101.v20160316</version>
33+
<version>2.0.9.106.v20160420</version>
3434
</plugin>
3535
<plugin>
3636
<groupId>org.apache.maven.plugins</groupId>

managed_vms/helloworld/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<plugin>
3535
<groupId>com.google.appengine</groupId>
3636
<artifactId>gcloud-maven-plugin</artifactId>
37-
<version>2.0.9.101.v20160316</version>
37+
<version>2.0.9.106.v20160420</version>
3838
</plugin>
3939
<plugin>
4040
<groupId>org.apache.maven.plugins</groupId>

managed_vms/mailgun/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<plugin>
4848
<groupId>com.google.appengine</groupId>
4949
<artifactId>gcloud-maven-plugin</artifactId>
50-
<version>2.0.9.101.v20160316</version>
50+
<version>2.0.9.106.v20160420</version>
5151
</plugin>
5252
<plugin>
5353
<groupId>org.apache.maven.plugins</groupId>

managed_vms/mailjet/pom.xml

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@
2727
</parent>
2828

2929
<dependencies>
30-
<dependency>
31-
<groupId>com.mailjet</groupId>
32-
<artifactId>mailjet-client</artifactId>
33-
<version>3.1.1</version>
34-
<scope>system</scope>
35-
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/client-3.1.1-jar-with-dependencies.jar</systemPath>
36-
</dependency>
37-
3830
<dependency>
3931
<groupId>javax.servlet</groupId>
4032
<artifactId>javax.servlet-api</artifactId>
@@ -44,19 +36,9 @@
4436
</dependency>
4537
<!-- [START dependencies] -->
4638
<dependency>
47-
<groupId>com.sun.jersey</groupId>
48-
<artifactId>jersey-core</artifactId>
49-
<version>1.19.1</version>
50-
</dependency>
51-
<dependency>
52-
<groupId>com.sun.jersey</groupId>
53-
<artifactId>jersey-client</artifactId>
54-
<version>1.19.1</version>
55-
</dependency>
56-
<dependency>
57-
<groupId>com.sun.jersey.contribs</groupId>
58-
<artifactId>jersey-multipart</artifactId>
59-
<version>1.19.1</version>
39+
<groupId>com.mailjet</groupId>
40+
<artifactId>mailjet-client</artifactId>
41+
<version>4.0.1</version>
6042
</dependency>
6143
<!-- [END dependencies] -->
6244
</dependencies>
@@ -67,7 +49,7 @@
6749
<plugin>
6850
<groupId>com.google.appengine</groupId>
6951
<artifactId>gcloud-maven-plugin</artifactId>
70-
<version>2.0.9.101.v20160316</version>
52+
<version>2.0.9.106.v20160420</version>
7153
</plugin>
7254
<plugin>
7355
<groupId>org.apache.maven.plugins</groupId>

managed_vms/memcache/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<plugin>
3838
<groupId>com.google.appengine</groupId>
3939
<artifactId>gcloud-maven-plugin</artifactId>
40-
<version>2.0.9.101.v20160316</version>
40+
<version>2.0.9.106.v20160420</version>
4141
</plugin>
4242
<plugin>
4343
<groupId>org.apache.maven.plugins</groupId>

0 commit comments

Comments
 (0)