|
| 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.appengine.mailjet; |
| 18 | + |
| 19 | +import com.mailjet.client.MailjetClient; |
| 20 | +import com.mailjet.client.MailjetRequest; |
| 21 | +import com.mailjet.client.MailjetResponse; |
| 22 | +import com.mailjet.client.errors.MailjetException; |
| 23 | +import com.mailjet.client.resource.Email; |
| 24 | + |
| 25 | +import org.json.JSONArray; |
| 26 | +import org.json.JSONObject; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +import javax.servlet.ServletException; |
| 31 | +import javax.servlet.http.HttpServlet; |
| 32 | +import javax.servlet.http.HttpServletRequest; |
| 33 | +import javax.servlet.http.HttpServletResponse; |
| 34 | + |
| 35 | +@SuppressWarnings("serial") |
| 36 | +public class MailjetServlet extends HttpServlet { |
| 37 | + private static final String MAILJET_API_KEY = System.getenv("MAILJET_API_KEY"); |
| 38 | + private static final String MAILJET_SECRET_KEY = System.getenv("MAILJET_SECRET_KEY"); |
| 39 | + private MailjetClient client = new MailjetClient(MAILJET_API_KEY, MAILJET_SECRET_KEY); |
| 40 | + |
| 41 | + @Override |
| 42 | + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, |
| 43 | + ServletException { |
| 44 | + String recipient = req.getParameter("to"); |
| 45 | + String sender = req.getParameter("from"); |
| 46 | + |
| 47 | + MailjetRequest email = new MailjetRequest(Email.resource) |
| 48 | + .property(Email.FROMEMAIL, sender) |
| 49 | + .property(Email.FROMNAME, "pandora") |
| 50 | + .property(Email.SUBJECT, "Your email flight plan!") |
| 51 | + .property(Email.TEXTPART, |
| 52 | + "Dear passenger, welcome to Mailjet! May the delivery force be with you!") |
| 53 | + .property(Email.HTMLPART, |
| 54 | + "<h3>Dear passenger, welcome to Mailjet!</h3><br/>May the delivery force be with you!") |
| 55 | + .property(Email.RECIPIENTS, new JSONArray().put(new JSONObject().put("Email", recipient))); |
| 56 | + |
| 57 | + try { |
| 58 | + // trigger the API call |
| 59 | + MailjetResponse response = client.post(email); |
| 60 | + // Read the response data and status |
| 61 | + resp.getWriter().print(response.getStatus()); |
| 62 | + resp.getWriter().print(response.getData()); |
| 63 | + } catch (MailjetException e) { |
| 64 | + throw new ServletException("Mailjet Exception", e); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments