Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add multi-recipient examples #638

Merged
merged 3 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ This documentation provides examples for specific use cases. Please [open an iss
# Use Cases

* [Send Mail Examples](examples/helpers/mail/Example.java)
* [Send a Single Email to Multiple Recipients](examples/helpers/mail/SingleEmailMultipleRecipients.java)
* [Send Multiple Emails to Multiple Recipients](examples/helpers/mail/MultipleEmailsMultipleRecipients.java)
* [Transactional Templates](#transactional-templates)
* [Legacy Templates](#legacy-templates)
* [How to Setup a Domain Authentication](#domain-authentication)
Expand Down
54 changes: 54 additions & 0 deletions examples/helpers/mail/MultipleEmailsMultipleRecipients.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import com.sendgrid.helpers.mail.objects.Personalization;

import java.io.IOException;

public class MultipleEmailsMultipleRecipients {

public static void main(String[] args) throws IOException {
final Mail mail = new Mail();

mail.setFrom(new Email("test@example.com", "Example User"));
mail.setSubject("Sending with Twilio SendGrid is Fun");

// Details on how to send an email with dynamic transactional templates:
// https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/
mail.setTemplateId("d-12345678901234567890123456789012");
childish-sambino marked this conversation as resolved.
Show resolved Hide resolved

final Personalization personalization1 = new Personalization();
personalization1.addTo(new Email("test1@example.com", "Example User1"));
personalization1.addDynamicTemplateData("name", "Example User1");
personalization1.addDynamicTemplateData("city", "Denver");
mail.addPersonalization(personalization1);

final Personalization personalization2 = new Personalization();
personalization2.addTo(new Email("test2@example.com", "Example User2"));
personalization2.addDynamicTemplateData("name", "Example User2");
personalization2.addDynamicTemplateData("city", "San Francisco");
mail.addPersonalization(personalization2);

mail.addContent(new Content("text/plain", "and easy to do anywhere, even with Java"));
childish-sambino marked this conversation as resolved.
Show resolved Hide resolved
mail.addContent(new Content("text/html", "<strong>and easy to do anywhere, even with Java</strong>"));

send(mail);
}

private static void send(final Mail mail) throws IOException {
final SendGrid client = new SendGrid(System.getenv("SENDGRID_API_KEY"));
final Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());

final Response response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
}
}
44 changes: 44 additions & 0 deletions examples/helpers/mail/SingleEmailMultipleRecipients.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import com.sendgrid.helpers.mail.objects.Personalization;

import java.io.IOException;

public class SingleEmailMultipleRecipients {

public static void main(String[] args) throws IOException {
final Mail mail = new Mail();

mail.setFrom(new Email("test@example.com", "Example User"));
mail.setSubject("Sending with Twilio SendGrid is Fun");

final Personalization personalization = new Personalization();
personalization.addTo(new Email("test1@example.com", "Example User1"));
personalization.addTo(new Email("test2@example.com", "Example User2"));
personalization.addTo(new Email("test3@example.com", "Example User3"));
mail.addPersonalization(personalization);

mail.addContent(new Content("text/plain", "and easy to do anywhere, even with Java"));
mail.addContent(new Content("text/html", "<strong>and easy to do anywhere, even with Java</strong>"));

send(mail);
}

private static void send(final Mail mail) throws IOException {
final SendGrid client = new SendGrid(System.getenv("SENDGRID_API_KEY"));
final Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());

final Response response = client.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
}
}