Skip to content
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Add the following to your build.gradle file in the root of your project.
...
dependencies {
...
compile 'com.sendgrid:sendgrid-java:0.3.1'
compile 'com.sendgrid:sendgrid-java:1.0.0'
}

repositories {
Expand Down Expand Up @@ -131,11 +131,11 @@ email.setHtml("<h1>My first email through SendGrid");
### Attachments

```java
email.addAttachment("contents", "text.txt");
email.addAttachment("text.txt", "contents");
// or
email.addAttachment(new File("./file.txt"), "text.txt");
email.addAttachment("image.png", new File("./image.png"));
// or
email.addAttachment(new InputStream(new File("./file.txt")), "text.txt");
email.addAttachment("text.txt", new InputStream(new File("./file.txt")));
```

## [X-SMTPAPI](http://sendgrid.com/docs/API_Reference/SMTP_API/index.html)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ apply plugin: 'maven'
apply plugin: 'signing'

group = 'com.sendgrid'
version = "0.3.1"
version = "1.0.0"
ext.packaging = 'jar'

if (!hasProperty("sonatypeUsername")) {
Expand Down
57 changes: 29 additions & 28 deletions src/main/java/com/sendgrid/SendGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static class Email {
public Map headers = new HashMap();

public Email () {
this.smtpapi = new SMTPAPI();
this.smtpapi = new SMTPAPI();
}

public Email addTo(String to) {
Expand Down Expand Up @@ -122,52 +122,53 @@ public Email setHtml(String html) {
return this;
}

public Email addSubstitution(String key, String[] val) {
this.smtpapi.addSubstitutions(key, val);
return this;
public Email addSubstitution(String key, String[] val) {
this.smtpapi.addSubstitutions(key, val);
return this;
}
public Email addUniqueArg(String key, String val) {

public Email addUniqueArg(String key, String val) {
this.smtpapi.addUniqueArg(key, val);
return this;
}
public Email addCategory(String category) {

public Email addCategory(String category) {
this.smtpapi.addCategory(category);
return this;
}
public Email addSection(String key, String val) {
this.smtpapi.addSection(key, val);

public Email addSection(String key, String val) {
this.smtpapi.addSection(key, val);
return this;
}

public Email addFilter(String filter_name, String parameter_name, String parameter_value) {
this.smtpapi.addFilter(filter_name, parameter_name, parameter_value);
return this;
this.smtpapi.addFilter(filter_name, parameter_name, parameter_value);
return this;
}

public Email addAttachment(File file, String name) throws FileNotFoundException {
return this.addAttachment(new FileInputStream(file), name);
public Email addAttachment(String name, File file) {
this.attachments.put(name, file);
return this;
}

public Email addAttachment(String file, String name) {
public Email addAttachment(String name, String file) {
this.attachments.put(name, file);
return this;
}

public Email addAttachment(InputStream file, String name) {
public Email addAttachment(String name, InputStream file) {
Scanner scanner = new Scanner(file, "UTF-8");
String buffer = new String();
while (scanner.hasNextLine()) {
buffer += scanner.nextLine();
}
scanner.close();
return this.addAttachment(buffer, name);
return this.addAttachment(name, buffer);
}
public Email addHeader(String key, String val) {
this.headers.put(key, val);

public Email addHeader(String key, String val) {
this.headers.put(key, val);
return this;
}

Expand All @@ -177,17 +178,17 @@ public Map toWebFormat() {
// updateMissingTo - There needs to be at least 1 to address,
// or else the mail won't send.
if ((this.to == null || this.to.isEmpty()) && this.from != null && !this.from.isEmpty()) {
String value = this.from;
String value = this.from;
body.put(PARAM_TO, value);
}

if (this.from != null && !this.from.isEmpty()) {
String value = this.from;
String value = this.from;
body.put(PARAM_FROM, value);
}

if (this.fromname != null && !this.fromname.isEmpty()) {
String value = this.fromname;
String value = this.fromname;
body.put(PARAM_FROMNAME, value);
}

Expand Down Expand Up @@ -217,14 +218,14 @@ public Map toWebFormat() {
body.put(PARAM_HTML, value);
}

if (!this.headers.isEmpty()) {
if (!this.headers.isEmpty()) {
JSONObject json_headers = new JSONObject(this.headers);
String serialized_headers = json_headers.toString();
body.put(PARAM_HEADERS, serialized_headers);
}
}

if (!this.smtpapi.jsonString().equals("{}")) {
String value = this.smtpapi.jsonString();
String value = this.smtpapi.jsonString();
body.put(PARAM_XSMTPAPI, value);
}

Expand Down
13 changes: 7 additions & 6 deletions src/test/java/com/sendgrid/SendGridTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class SendGridTest {
correct.put("to", address);

assertEquals(correct, email.toWebFormat());
}
}

@Test public void testSetFromName() {
email = new SendGrid.Email();
Expand Down Expand Up @@ -145,7 +145,7 @@ public class SendGridTest {
email = new SendGrid.Email();

email.addHeader("key", "value");
email.addHeader("other", "other-value");
email.addHeader("other", "other-value");

Map correct = new HashMap();
correct.put("headers", "{\"other\":\"other-value\",\"key\":\"value\"}");
Expand All @@ -158,10 +158,10 @@ public class SendGridTest {
email = new SendGrid.Email();

File file = new File(getClass().getResource("/test.txt").getFile());
email.addAttachment(file, "test.txt");
email.addAttachment("test.txt", file);

Map correct = new HashMap();
correct.put("files[test.txt]", "This is a test file.");
correct.put("files[test.txt]", file);

assertEquals(correct, email.toWebFormat());
}
Expand All @@ -176,11 +176,12 @@ public class SendGridTest {
email.setText("Test body");
email.setHtml("Test body");
email.addCategory("-TEST-");
File file = new File(getClass().getResource("/test.txt").getFile());
email.addAttachment(file, "test.txt");
File file = new File(getClass().getResource("/image.png").getFile());
email.addAttachment("image.png", file);
SendGrid sendgrid = new SendGrid(USERNAME, PASSWORD);
SendGrid.Response resp = sendgrid.send(email);

assertEquals("{\"message\":\"error\",\"errors\":[\"Bad username / password\"]}", resp.getMessage());
}

}