|
| 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 | + |
| 17 | +package com.example.appengine.mailgun; |
| 18 | + |
| 19 | +import com.sun.jersey.api.client.Client; |
| 20 | +import com.sun.jersey.api.client.ClientResponse; |
| 21 | +import com.sun.jersey.api.client.WebResource; |
| 22 | +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; |
| 23 | +import com.sun.jersey.core.util.MultivaluedMapImpl; |
| 24 | +import com.sun.jersey.multipart.FormDataMultiPart; |
| 25 | +import com.sun.jersey.multipart.file.FileDataBodyPart; |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +import javax.servlet.annotation.WebServlet; |
| 31 | +import javax.servlet.http.HttpServlet; |
| 32 | +import javax.servlet.http.HttpServletRequest; |
| 33 | +import javax.servlet.http.HttpServletResponse; |
| 34 | +import javax.ws.rs.core.MediaType; |
| 35 | + |
| 36 | +// [START example] |
| 37 | +@SuppressWarnings("serial") |
| 38 | +@WebServlet(name = "mailgun", value = "/send/email") |
| 39 | +public class MailgunServlet extends HttpServlet { |
| 40 | + |
| 41 | + private static final String MAILGUN_DOMAIN_NAME = System.getenv("MAILGUN_DOMAIN_NAME"); |
| 42 | + private static final String MAILGUN_API_KEY = System.getenv("MAILGUN_API_KEY"); |
| 43 | + |
| 44 | + @Override |
| 45 | + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 46 | + String type = req.getParameter("submit"); |
| 47 | + String recipient = req.getParameter("to"); |
| 48 | + ClientResponse clientResponse; |
| 49 | + if (type.equals("Send simple email")) { |
| 50 | + clientResponse = sendSimpleMessage(recipient); |
| 51 | + } else { |
| 52 | + clientResponse = sendComplexMessage(recipient); |
| 53 | + } |
| 54 | + if (clientResponse.getStatus() == 200) { |
| 55 | + resp.getWriter().print("Email sent."); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // [START simple] |
| 60 | + private ClientResponse sendSimpleMessage(String recipient) { |
| 61 | + Client client = Client.create(); |
| 62 | + client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY)); |
| 63 | + WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME |
| 64 | + + "/messages"); |
| 65 | + MultivaluedMapImpl formData = new MultivaluedMapImpl(); |
| 66 | + formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">"); |
| 67 | + formData.add("to", recipient); |
| 68 | + formData.add("subject", "Simple Mailgun Example"); |
| 69 | + formData.add("text", "Plaintext content"); |
| 70 | + return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, |
| 71 | + formData); |
| 72 | + } |
| 73 | + // [END simple] |
| 74 | + |
| 75 | + // [START complex] |
| 76 | + private ClientResponse sendComplexMessage(String recipient) { |
| 77 | + Client client = Client.create(); |
| 78 | + client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY)); |
| 79 | + WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME |
| 80 | + + "/messages"); |
| 81 | + FormDataMultiPart formData = new FormDataMultiPart(); |
| 82 | + formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">"); |
| 83 | + formData.field("to", recipient); |
| 84 | + formData.field("subject", "Complex Mailgun Example"); |
| 85 | + formData.field("html", "<html>HTML <strong>content</strong></html>"); |
| 86 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 87 | + File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile()); |
| 88 | + formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE)); |
| 89 | + return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE) |
| 90 | + .post(ClientResponse.class, formData); |
| 91 | + } |
| 92 | + // [END complex] |
| 93 | +} |
| 94 | +// [END example] |
0 commit comments