Skip to content

Commit cc1918f

Browse files
author
Shun Fan
committed
Add mailjet samples
Remove unused annotation in mailgun
1 parent 2e672da commit cc1918f

File tree

14 files changed

+393
-2
lines changed

14 files changed

+393
-2
lines changed

appengine/mailgun/src/main/java/com/example/appengine/mailgun/MailgunServlet.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727
import java.io.File;
2828
import java.io.IOException;
2929

30-
import javax.servlet.annotation.WebServlet;
3130
import javax.servlet.http.HttpServlet;
3231
import javax.servlet.http.HttpServletRequest;
3332
import javax.servlet.http.HttpServletResponse;
3433
import javax.ws.rs.core.MediaType;
3534

3635
// [START example]
3736
@SuppressWarnings("serial")
38-
@WebServlet(name = "mailgun", value = "/send/email")
3937
public class MailgunServlet extends HttpServlet {
4038

4139
private static final String MAILGUN_DOMAIN_NAME = System.getenv("MAILGUN_DOMAIN_NAME");

appengine/mailjet/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Mailjet sample for Google App Engine
2+
This sample demonstrates how to use [Mailjet](https://www.mailjet.com/) on Google App Engine.
3+
4+
## Setup
5+
1. Before using, ensure the address you plan to send from has been verified in Mailjet.
6+
7+
## Running locally
8+
$ export MAILJET_API_KEY=[your mailjet api key]
9+
$ export MAILJET_SECRET_KEY=[your mailjet secret key]
10+
$ mvn clean appengine:devserver
11+
12+
## Deploying
13+
1. Edit the environment variables in the appengine-web.xml with the appropriate Mailjet values.
14+
$ mvn clean appengine:update

appengine/mailjet/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!--
2+
Copyright 2015 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>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-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+
<dependencies>
29+
<dependency>
30+
<groupId>com.mailjet</groupId>
31+
<artifactId>mailjet-client</artifactId>
32+
<version>3.1.1</version>
33+
<scope>system</scope>
34+
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/client-3.1.1-jar-with-dependencies.jar</systemPath>
35+
</dependency>
36+
<dependency>
37+
<groupId>javax.servlet</groupId>
38+
<artifactId>javax.servlet-api</artifactId>
39+
<version>3.1.0</version>
40+
<type>jar</type>
41+
<scope>provided</scope>
42+
</dependency>
43+
<!-- [START dependencies] -->
44+
<dependency>
45+
<groupId>com.sun.jersey</groupId>
46+
<artifactId>jersey-core</artifactId>
47+
<version>1.19.1</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.sun.jersey</groupId>
51+
<artifactId>jersey-client</artifactId>
52+
<version>1.19.1</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.sun.jersey.contribs</groupId>
56+
<artifactId>jersey-multipart</artifactId>
57+
<version>1.19.1</version>
58+
</dependency>
59+
<!-- [END dependencies] -->
60+
</dependencies>
61+
<build>
62+
<!-- for hot reload of the web application -->
63+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
64+
<plugins>
65+
<plugin>
66+
<groupId>com.google.appengine</groupId>
67+
<artifactId>appengine-maven-plugin</artifactId>
68+
<version>${appengine.sdk.version}</version>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.appengine.mailjet;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
import com.mailjet.client.MailjetClient;
7+
import com.mailjet.client.MailjetRequest;
8+
import com.mailjet.client.MailjetResponse;
9+
import com.mailjet.client.errors.MailjetException;
10+
import com.mailjet.client.resource.Email;
11+
12+
import java.io.IOException;
13+
14+
import javax.servlet.ServletException;
15+
import javax.servlet.http.HttpServlet;
16+
import javax.servlet.http.HttpServletRequest;
17+
import javax.servlet.http.HttpServletResponse;
18+
19+
@SuppressWarnings("serial")
20+
public class MailjetServlet extends HttpServlet {
21+
private static final String MAILJET_API_KEY = System.getenv("MAILJET_API_KEY");
22+
private static final String MAILJET_SECRET_KEY = System.getenv("MAILJET_SECRET_KEY");
23+
private MailjetClient client = new MailjetClient(MAILJET_API_KEY, MAILJET_SECRET_KEY);
24+
25+
@Override
26+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
27+
ServletException {
28+
String recipient = req.getParameter("to");
29+
String sender = req.getParameter("from");
30+
31+
MailjetRequest email = new MailjetRequest(Email.resource)
32+
.property(Email.FROMEMAIL, sender)
33+
.property(Email.FROMNAME, "pandora")
34+
.property(Email.SUBJECT, "Your email flight plan!")
35+
.property(Email.TEXTPART,
36+
"Dear passenger, welcome to Mailjet! May the delivery force be with you!")
37+
.property(Email.HTMLPART,
38+
"<h3>Dear passenger, welcome to Mailjet!</h3><br/>May the delivery force be with you!")
39+
.property(Email.RECIPIENTS,
40+
new JSONArray().put(new JSONObject().put("Email", recipient)));
41+
42+
try {
43+
// trigger the API call
44+
MailjetResponse response = client.post(email);
45+
// Read the response data and status
46+
resp.getWriter().print(response.getStatus());
47+
resp.getWriter().print(response.getData());
48+
} catch (MailjetException e) {
49+
throw new ServletException("Mailjet Exception", e);
50+
}
51+
}
52+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3+
<application>YOUR-PROJECT-ID</application>
4+
<version>YOUR-VERSION-ID</version>
5+
<threadsafe>true</threadsafe>
6+
<env-variables>
7+
<env-var name="MAILJET_API_KEY" value="YOUR-MAILJET-API-KEY" />
8+
<env-var name="MAILJET_SECRET_KEY" value="YOUR-MAILJET-SECRET-KEY" />
9+
</env-variables>
10+
</appengine-web-app>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
version="2.5">
6+
<servlet>
7+
<servlet-name>mailjet</servlet-name>
8+
<servlet-class>com.example.appengine.mailjet.MailjetServlet</servlet-class>
9+
</servlet>
10+
<servlet-mapping>
11+
<servlet-name>mailjet</servlet-name>
12+
<url-pattern>/send/email</url-pattern>
13+
</servlet-mapping>
14+
</web-app>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!doctype html>
2+
<!--
3+
Copyright 2015 Google Inc. All Rights Reserved.
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+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<html>
15+
<head>
16+
<title>Mailgun on Google App Engine Managed VMs</title>
17+
</head>
18+
<body>
19+
<!-- [START form] -->
20+
<form method="post" action="/send/email">
21+
<input type="text" name="to" placeholder="Enter recipient email">
22+
<input type="text" name="from" placeholder="Enter sender email">
23+
<input type="submit" name="submit" value="Send email">
24+
</form>
25+
<!-- [END form] -->
26+
</body>
27+
</html>

managed_vms/mailjet/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Mailjet sample for Google Managed VMs
2+
This sample demonstrates how to use [Mailjet](https://www.mailjet.com/) on Google Managed VMs.
3+
4+
## Setup
5+
1. Before using, ensure the address you plan to send from has been verified in Mailjet.
6+
7+
## Running locally
8+
$ export MAILJET_API_KEY=[your mailjet api key]
9+
$ export MAILJET_SECRET_KEY=[your mailjet secret key]
10+
$ mvn clean jetty:run
11+
12+
## Deploying
13+
1. Edit the environment variables in the app.yaml with the appropriate Mailjet values.
14+
$ mvn clean gcloud:deploy

managed_vms/mailjet/pom.xml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!--
2+
Copyright 2015 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>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.managedvms</groupId>
21+
<artifactId>managed-vms-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>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+
38+
<dependency>
39+
<groupId>javax.servlet</groupId>
40+
<artifactId>javax.servlet-api</artifactId>
41+
<version>3.1.0</version>
42+
<type>jar</type>
43+
<scope>provided</scope>
44+
</dependency>
45+
<!-- [START dependencies] -->
46+
<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>
60+
</dependency>
61+
<!-- [END dependencies] -->
62+
</dependencies>
63+
<build>
64+
<!-- for hot reload of the web application -->
65+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
66+
<plugins>
67+
<plugin>
68+
<groupId>com.google.appengine</groupId>
69+
<artifactId>gcloud-maven-plugin</artifactId>
70+
<version>2.0.9.101.v20160316</version>
71+
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-war-plugin</artifactId>
75+
<version>2.6</version>
76+
<configuration>
77+
<failOnMissingWebXml>false</failOnMissingWebXml>
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+
<plugin>
90+
<groupId>org.eclipse.jetty</groupId>
91+
<artifactId>jetty-maven-plugin</artifactId>
92+
<version>9.3.7.v20160115</version>
93+
</plugin>
94+
</plugins>
95+
</build>
96+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
runtime: java
2+
vm: true
3+
4+
handlers:
5+
- url: /.*
6+
script: this field is required, but ignored
7+
secure: always
8+
9+
# [START env_variables]
10+
env_variables:
11+
MAILJET_API_KEY: YOUR-MAILJET-API-KEY
12+
MAILJET_SECRET_KEY: YOUR-MAILJET-SECRET-KEY
13+
# [END env_variables]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.example.managedvms.mailjet;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
import com.mailjet.client.MailjetClient;
7+
import com.mailjet.client.MailjetRequest;
8+
import com.mailjet.client.MailjetResponse;
9+
import com.mailjet.client.errors.MailjetException;
10+
import com.mailjet.client.resource.Email;
11+
12+
import java.io.IOException;
13+
14+
import javax.servlet.ServletException;
15+
import javax.servlet.annotation.WebServlet;
16+
import javax.servlet.http.HttpServlet;
17+
import javax.servlet.http.HttpServletRequest;
18+
import javax.servlet.http.HttpServletResponse;
19+
20+
@SuppressWarnings("serial")
21+
@WebServlet(name = "mailjet", value = "/send/email")
22+
public class MailjetServlet extends HttpServlet {
23+
private static final String MAILJET_API_KEY = System.getenv("MAILJET_API_KEY");
24+
private static final String MAILJET_SECRET_KEY = System.getenv("MAILJET_SECRET_KEY");
25+
private MailjetClient client = new MailjetClient(MAILJET_API_KEY, MAILJET_SECRET_KEY);
26+
27+
@Override
28+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
29+
ServletException {
30+
String recipient = req.getParameter("to");
31+
String sender = req.getParameter("from");
32+
33+
MailjetRequest email = new MailjetRequest(Email.resource)
34+
.property(Email.FROMEMAIL, sender)
35+
.property(Email.FROMNAME, "pandora")
36+
.property(Email.SUBJECT, "Your email flight plan!")
37+
.property(Email.TEXTPART,
38+
"Dear passenger, welcome to Mailjet! May the delivery force be with you!")
39+
.property(Email.HTMLPART,
40+
"<h3>Dear passenger, welcome to Mailjet!</h3><br/>May the delivery force be with you!")
41+
.property(Email.RECIPIENTS,
42+
new JSONArray().put(new JSONObject().put("Email", recipient)));
43+
44+
try {
45+
// trigger the API call
46+
MailjetResponse response = client.post(email);
47+
// Read the response data and status
48+
resp.getWriter().print(response.getStatus());
49+
resp.getWriter().print(response.getData());
50+
} catch (MailjetException e) {
51+
throw new ServletException("Mailjet Exception", e);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)