|
1 | 1 | /*
|
2 |
| - * Copyright 2015 Google Inc. |
| 2 | + * Copyright 2018 Google LLC |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package com.example.appengine.sendgrid;
|
18 | 18 |
|
| 19 | +// [START gae_sendgrid_import] |
| 20 | +import com.sendgrid.Content; |
| 21 | +import com.sendgrid.Email; |
| 22 | +import com.sendgrid.Mail; |
| 23 | +import com.sendgrid.Method; |
| 24 | +import com.sendgrid.Request; |
| 25 | +import com.sendgrid.Response; |
19 | 26 | import com.sendgrid.SendGrid;
|
20 |
| -import com.sendgrid.SendGridException; |
21 | 27 | import java.io.IOException;
|
22 | 28 | import javax.servlet.ServletException;
|
23 | 29 | import javax.servlet.http.HttpServlet;
|
24 | 30 | import javax.servlet.http.HttpServletRequest;
|
25 | 31 | import javax.servlet.http.HttpServletResponse;
|
| 32 | +// [END gae_sendgrid_import] |
26 | 33 |
|
27 |
| -// [START example] |
28 | 34 | @SuppressWarnings("serial")
|
29 | 35 | public class SendEmailServlet extends HttpServlet {
|
30 | 36 |
|
31 | 37 | @Override
|
32 |
| - public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, |
33 |
| - ServletException { |
| 38 | + public void service(HttpServletRequest req, HttpServletResponse resp) |
| 39 | + throws IOException, ServletException { |
| 40 | + // Get parameters from environment variables. |
34 | 41 | final String sendgridApiKey = System.getenv("SENDGRID_API_KEY");
|
35 | 42 | final String sendgridSender = System.getenv("SENDGRID_SENDER");
|
| 43 | + |
| 44 | + // Get email from query string. |
36 | 45 | final String toEmail = req.getParameter("to");
|
37 | 46 | if (toEmail == null) {
|
38 | 47 | resp.getWriter()
|
39 | 48 | .print("Please provide an email address in the \"to\" query string parameter.");
|
40 | 49 | return;
|
41 | 50 | }
|
42 | 51 |
|
| 52 | + // [START gae_sendgrid] |
| 53 | + // Set content for request. |
| 54 | + Email to = new Email(toEmail); |
| 55 | + Email from = new Email(sendgridSender); |
| 56 | + String subject = "This is a test email"; |
| 57 | + Content content = new Content("text/plain", "Example text body."); |
| 58 | + Mail mail = new Mail(from, subject, to, content); |
| 59 | + |
| 60 | + // Instantiates SendGrid client. |
43 | 61 | SendGrid sendgrid = new SendGrid(sendgridApiKey);
|
44 |
| - SendGrid.Email email = new SendGrid.Email(); |
45 |
| - email.addTo(toEmail); |
46 |
| - email.setFrom(sendgridSender); |
47 |
| - email.setSubject("This is a test email"); |
48 |
| - email.setText("Example text body."); |
| 62 | + |
| 63 | + // Instantiate SendGrid request. |
| 64 | + Request request = new Request(); |
49 | 65 |
|
50 | 66 | try {
|
51 |
| - SendGrid.Response response = sendgrid.send(email); |
52 |
| - if (response.getCode() != 200) { |
53 |
| - resp.getWriter().print(String.format("An error occured: %s", response.getMessage())); |
| 67 | + // Set request configuration. |
| 68 | + request.setMethod(Method.POST); |
| 69 | + request.setEndpoint("mail/send"); |
| 70 | + request.setBody(mail.build()); |
| 71 | + |
| 72 | + // Use the client to send the API request. |
| 73 | + Response response = sendgrid.api(request); |
| 74 | + |
| 75 | + if (response.getStatusCode() != 202) { |
| 76 | + resp.getWriter().print(String.format("An error occured: %s", response.getStatusCode())); |
54 | 77 | return;
|
55 | 78 | }
|
| 79 | + |
| 80 | + // Print response. |
56 | 81 | resp.getWriter().print("Email sent.");
|
57 |
| - } catch (SendGridException e) { |
| 82 | + } catch (IOException e) { |
58 | 83 | throw new ServletException("SendGrid error", e);
|
59 | 84 | }
|
| 85 | + // [END gae_sendgrid] |
60 | 86 | }
|
61 | 87 | }
|
62 |
| -// [END example] |
|
0 commit comments