/flexible/mailjet/src/main/java/com/example/mailjet/MailjetServlet.java

https://github.com/GoogleCloudPlatform/java-docs-samples · Java · 83 lines · 54 code · 8 blank · 21 comment · 0 complexity · 86a091832b3f320cc7791938e1f8bbea MD5 · raw file

  1. /*
  2. * Copyright 2016 Google Inc.
  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. // [START gae_flex_mailjet_config]
  17. package com.example.mailjet;
  18. import com.mailjet.client.ClientOptions;
  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.errors.MailjetSocketTimeoutException;
  24. import com.mailjet.client.resource.Emailv31;
  25. import java.io.IOException;
  26. import javax.servlet.ServletException;
  27. import javax.servlet.annotation.WebServlet;
  28. import javax.servlet.http.HttpServlet;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import org.json.JSONArray;
  32. import org.json.JSONObject;
  33. // [END gae_flex_mailjet_config]
  34. // [START gae_flex_mailjet_send_message]
  35. @SuppressWarnings("serial")
  36. @WebServlet(name = "mailjet", value = "/send/email")
  37. public class MailjetServlet extends HttpServlet {
  38. private static final String MAILJET_API_KEY = System.getenv("MAILJET_API_KEY");
  39. private static final String MAILJET_SECRET_KEY = System.getenv("MAILJET_SECRET_KEY");
  40. private MailjetClient client =
  41. new MailjetClient(MAILJET_API_KEY, MAILJET_SECRET_KEY, new ClientOptions("v3.1"));
  42. @Override
  43. public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
  44. ServletException {
  45. String recipient = req.getParameter("to");
  46. String sender = req.getParameter("from");
  47. MailjetRequest email = new MailjetRequest(Emailv31.resource)
  48. .property(Emailv31.MESSAGES, new JSONArray()
  49. .put(new JSONObject()
  50. .put(Emailv31.Message.FROM, new JSONObject()
  51. .put("Email", sender)
  52. .put("Name", "Mailjet Pilot"))
  53. .put(Emailv31.Message.TO, new JSONArray()
  54. .put(new JSONObject()
  55. .put("Email", recipient)))
  56. .put(Emailv31.Message.SUBJECT, "Your email flight plan!")
  57. .put(Emailv31.Message.TEXTPART,
  58. "Dear passenger, welcome to Mailjet! May the delivery force be with you!")
  59. .put(Emailv31.Message.HTMLPART,
  60. "<h3>Dear passenger, welcome to Mailjet!</h3><br />"
  61. + "May the delivery force be with you!")));
  62. try {
  63. // trigger the API call
  64. MailjetResponse response = client.post(email);
  65. // Read the response data and status
  66. resp.getWriter().print(response.getStatus());
  67. resp.getWriter().print(response.getData());
  68. } catch (MailjetException e) {
  69. throw new ServletException("Mailjet Exception", e);
  70. } catch (MailjetSocketTimeoutException e) {
  71. throw new ServletException("Mailjet socket timed out", e);
  72. }
  73. }
  74. }
  75. // [END gae_flex_mailjet_send_message]