Skip to content

Refactored mail helper and added multiple mail send. #255

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

Closed
wants to merge 19 commits into from
Closed
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ examples/Example.java
.settings
.classpath
.project
.vscode
scripts/prism
.env
66 changes: 31 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,25 @@ import com.sendgrid.*;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
Email from = new Email("[email protected]");
String subject = "Sending with SendGrid is Fun";
Email to = new Email("[email protected]");
Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
Mail mail = new Mail(from, subject, to, content);

SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
public static void main(String[] args) throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Response response = new Mail()
.from("[email protected]")
.subject("Sending with SendGrid is Fun")
.to(
new Email()
.email("[email protected]")
.name("John Doe")
).content(
new Content()
.type(ContentType.TEXT_PLAIN)
.value("and easy to do anywhere, even with Java")
).send(sg);

System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
}
}
}
```

Expand All @@ -140,21 +138,19 @@ import com.sendgrid.*;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
try {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody("{\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Sending with SendGrid is Fun\"}],\"from\":{\"email\":\"[email protected]\"},\"content\":[{\"type\":\"text/plain\",\"value\": \"and easy to do anywhere, even with Java\"}]}");
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
public static void main(String[] args) throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();

request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody("{\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Sending with SendGrid is Fun\"}],\"from\":{\"email\":\"[email protected]\"},\"content\":[{\"type\":\"text/plain\",\"value\": \"and easy to do anywhere, even with Java\"}]}");

Response response = sg.send(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
}
}
}
```

Expand All @@ -171,7 +167,7 @@ public class Example {
Request request = new Request();
request.setMethod(Method.GET);
request.setEndpoint("api_keys");
Response response = sg.api(request);
Response response = sg.send(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
Expand Down
465 changes: 235 additions & 230 deletions USAGE.md

Large diffs are not rendered by default.

71 changes: 34 additions & 37 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,29 @@ import com.sendgrid.*;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
Email from = new Email("[email protected]");
String subject = "I'm replacing the subject tag";
Email to = new Email("[email protected]");
Content content = new Content("text/html", "I'm replacing the <strong>body tag</strong>");
Mail mail = new Mail(from, subject, to, content);
mail.personalization.get(0).addSubstitution("-name-", "Example User");
mail.personalization.get(0).addSubstitution("-city-", "Denver");
mail.setTemplateId("13b8f94f-bcae-4ec6-b752-70d6cb59f932");

SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
public static void main(String[] args) throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Response response = new Mail()
.from("[email protected]")
.subject("I'm replacing the subject tag")
.to(
new Email()
.email("[email protected]")
).content(
new Content()
.type(ContentType.TEXT_HTML)
.value("I'm replacing the <strong>body tag</strong>")
).personalization(
new Personalization()
.substitution("-name-", "Example User")
.substitution("-city-", "Denver")
).templateId("13b8f94f-bcae-4ec6-b752-70d6cb59f932")
.send(sg);

System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
}
}
}
```

Expand All @@ -84,21 +83,19 @@ import com.sendgrid.*;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
try {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody("{\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"substitutions\":{\"-name-\":\"Example User\",\"-city-\":\"Denver\"},\"subject\":\"Hello World from the SendGrid Java Library!\"}],\"from\":{\"email\":\"[email protected]\"},\"content\":[{\"type\":\"text/html\",\"value\": \"I'm replacing the <strong>body tag</strong>\"}],\"template_id\": \"13b8f94f-bcae-4ec6-b752-70d6cb59f932\"}");
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
public static void main(String[] args) throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();

request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody("{\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"substitutions\":{\"-name-\":\"Example User\",\"-city-\":\"Denver\"},\"subject\":\"Hello World from the SendGrid Java Library!\"}],\"from\":{\"email\":\"[email protected]\"},\"content\":[{\"type\":\"text/html\",\"value\": \"I'm replacing the <strong>body tag</strong>\"}],\"template_id\": \"13b8f94f-bcae-4ec6-b752-70d6cb59f932\"}");

Response response = sg.send(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
}
}
}
```

Expand Down
19 changes: 0 additions & 19 deletions bin/com/sendgrid/helpers/README.md

This file was deleted.

63 changes: 63 additions & 0 deletions src/main/java/com/sendgrid/ASM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.sendgrid;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Arrays;

/**
* A JSON-serializable model, describing how unsubscribes should
* be handled.
*/
@JsonInclude(Include.NON_DEFAULT)
public class ASM {

@JsonProperty("group_id")
private int groupId;

@JsonProperty("groups_to_display")
private int[] groupsToDisplay;

/**
* Gets the group ID.
*
* @return the group ID.
*/
@JsonProperty("group_id")
public int getGroupId() {
return groupId;
}

/**
* Sets the group ID.
*
* @param groupId the group ID.
* @return {@code this} for chaining.
*/
public ASM groupId(int groupId) {
this.groupId = groupId;
return this;
}

/**
* Gets the groups to display.
*
* @return the groups to display.
*/
@JsonProperty("groups_to_display")
public int[] getGroupsToDisplay() {
return groupsToDisplay;
}

/**
* Sets the groups to display.
*
* @param groupsToDisplay the groups to display.
* @return {@code this} for chaining.
*/
public ASM groupsToDisplay(int[] groupsToDisplay) {
this.groupsToDisplay = Arrays.copyOf(groupsToDisplay, groupsToDisplay.length);
return this;
}
}
Loading