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 mailjet sample #235

Merged
merged 2 commits into from
May 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 44 additions & 0 deletions compute/mailjet/README.md
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`
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we give them a script that does this? So they can create their instance and set it up?


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]`
91 changes: 91 additions & 0 deletions compute/mailjet/pom.xml
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>
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]
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());
}
}
2 changes: 1 addition & 1 deletion managed_vms/analytics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/async-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
<configuration>
<verbosity>debug</verbosity>
<log_level>debug</log_level>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/cloudstorage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/cron/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/disk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/extending-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/helloworld/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/mailgun/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
26 changes: 4 additions & 22 deletions managed_vms/mailjet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@
</parent>

<dependencies>
<dependency>
<groupId>com.mailjet</groupId>
<artifactId>mailjet-client</artifactId>
<version>3.1.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/client-3.1.1-jar-with-dependencies.jar</systemPath>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand All @@ -44,19 +36,9 @@
</dependency>
<!-- [START dependencies] -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.19.1</version>
<groupId>com.mailjet</groupId>
<artifactId>mailjet-client</artifactId>
<version>4.0.1</version>
</dependency>
<!-- [END dependencies] -->
</dependencies>
Expand All @@ -67,7 +49,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion managed_vms/memcache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.101.v20160316</version>
<version>2.0.9.106.v20160420</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Loading