Skip to content

Commit 4d69264

Browse files
Les Vogellesv
authored andcommitted
Use the latest sendgrid
Update to use the latest sendgrid.
1 parent 065585d commit 4d69264

File tree

3 files changed

+70
-27
lines changed

3 files changed

+70
-27
lines changed

appengine-java8/sendgrid/src/main/java/com/example/appengine/sendgrid/SendEmailServlet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
package com.example.appengine.sendgrid;
1818

1919
// [START gae_sendgrid_import]
20-
import com.sendgrid.Content;
21-
import com.sendgrid.Email;
22-
import com.sendgrid.Mail;
2320
import com.sendgrid.Method;
2421
import com.sendgrid.Request;
2522
import com.sendgrid.Response;
2623
import com.sendgrid.SendGrid;
24+
import com.sendgrid.helpers.mail.Mail;
25+
import com.sendgrid.helpers.mail.objects.Content;
26+
import com.sendgrid.helpers.mail.objects.Email;
2727
import java.io.IOException;
2828
import javax.servlet.ServletException;
2929
import javax.servlet.http.HttpServlet;

compute/sendgrid/src/main/java/com/example/compute/sendgrid/SendEmailServlet.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,50 @@
1616

1717
package com.example.compute.sendgrid;
1818

19+
import com.sendgrid.Method;
20+
import com.sendgrid.Request;
21+
import com.sendgrid.Response;
1922
import com.sendgrid.SendGrid;
20-
import com.sendgrid.SendGridException;
23+
import com.sendgrid.helpers.mail.Mail;
24+
import com.sendgrid.helpers.mail.objects.Content;
25+
import com.sendgrid.helpers.mail.objects.Email;
26+
import java.io.IOException;
2127

2228
// [START example]
2329
public class SendEmailServlet {
2430
static final String SENDGRID_API_KEY = "YOUR-SENDGRID-API-KEY";
2531
static final String SENDGRID_SENDER = "YOUR-SENDGRID-FROM-EMAIL";
2632
static final String TO_EMAIL = "DESTINATION-EMAIL";
2733

28-
public static void main(String[] args) throws SendGridException {
34+
public static void main(String[] args) throws IOException {
2935

36+
37+
// Set content for request.
38+
Email to = new Email(TO_EMAIL);
39+
Email from = new Email(SENDGRID_SENDER);
40+
String subject = "This is a test email";
41+
Content content = new Content("text/plain", "Example text body.");
42+
Mail mail = new Mail(from, subject, to, content);
43+
44+
// Instantiates SendGrid client.
3045
SendGrid sendgrid = new SendGrid(SENDGRID_API_KEY);
31-
SendGrid.Email email = new SendGrid.Email();
32-
email.addTo(TO_EMAIL);
33-
email.setFrom(SENDGRID_SENDER);
34-
email.setSubject("This is a test email");
35-
email.setText("Example text body.");
36-
37-
SendGrid.Response response = sendgrid.send(email);
38-
if (response.getCode() != 200) {
39-
System.out.print(String.format("An error occurred: %s", response.getMessage()));
46+
47+
// Instantiate SendGrid request.
48+
Request request = new Request();
49+
50+
// Set request configuration.
51+
request.setMethod(Method.POST);
52+
request.setEndpoint("mail/send");
53+
request.setBody(mail.build());
54+
55+
// Use the client to send the API request.
56+
Response response = sendgrid.api(request);
57+
58+
if (response.getStatusCode() != 202) {
59+
System.out.print(String.format("An error occurred: %s", response.getStatusCode()));
4060
return;
4161
}
62+
4263
System.out.print("Email sent.");
4364
}
4465

flexible/sendgrid/src/main/java/com/example/sendgrid/SendEmailServlet.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616

1717
package com.example.sendgrid;
1818

19+
import com.sendgrid.Method;
20+
import com.sendgrid.Request;
21+
import com.sendgrid.Response;
1922
import com.sendgrid.SendGrid;
20-
import com.sendgrid.SendGridException;
21-
23+
import com.sendgrid.helpers.mail.Mail;
24+
import com.sendgrid.helpers.mail.objects.Content;
25+
import com.sendgrid.helpers.mail.objects.Email;
2226
import java.io.IOException;
2327

2428
import javax.servlet.ServletException;
@@ -33,32 +37,50 @@
3337
public class SendEmailServlet extends HttpServlet {
3438

3539
@Override
36-
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException,
37-
ServletException {
40+
public void service(HttpServletRequest req, HttpServletResponse resp)
41+
throws IOException, ServletException {
42+
// Get parameters from environment variables.
3843
final String sendgridApiKey = System.getenv("SENDGRID_API_KEY");
3944
final String sendgridSender = System.getenv("SENDGRID_SENDER");
45+
46+
// Get email from query string.
4047
final String toEmail = req.getParameter("to");
4148
if (toEmail == null) {
4249
resp.getWriter()
4350
.print("Please provide an email address in the \"to\" query string parameter.");
4451
return;
4552
}
4653

54+
// Set content for request.
55+
Email to = new Email(toEmail);
56+
Email from = new Email(sendgridSender);
57+
String subject = "This is a test email";
58+
Content content = new Content("text/plain", "Example text body.");
59+
Mail mail = new Mail(from, subject, to, content);
60+
61+
// Instantiates SendGrid client.
4762
SendGrid sendgrid = new SendGrid(sendgridApiKey);
48-
SendGrid.Email email = new SendGrid.Email();
49-
email.addTo(toEmail);
50-
email.setFrom(sendgridSender);
51-
email.setSubject("This is a test email");
52-
email.setText("Example text body.");
63+
64+
// Instantiate SendGrid request.
65+
Request request = new Request();
5366

5467
try {
55-
SendGrid.Response response = sendgrid.send(email);
56-
if (response.getCode() != 200) {
57-
resp.getWriter().print(String.format("An error occurred: %s", response.getMessage()));
68+
// Set request configuration.
69+
request.setMethod(Method.POST);
70+
request.setEndpoint("mail/send");
71+
request.setBody(mail.build());
72+
73+
// Use the client to send the API request.
74+
Response response = sendgrid.api(request);
75+
76+
if (response.getStatusCode() != 202) {
77+
resp.getWriter().print(String.format("An error occurred: %s", response.getStatusCode()));
5878
return;
5979
}
80+
81+
// Print response.
6082
resp.getWriter().print("Email sent.");
61-
} catch (SendGridException e) {
83+
} catch (IOException e) {
6284
throw new ServletException("SendGrid error", e);
6385
}
6486
}

0 commit comments

Comments
 (0)