Skip to content

Commit 3f2ed3b

Browse files
committed
Merge pull request #29 from sendgrid/attachments
Attachments
2 parents fedf22f + 5f31500 commit 3f2ed3b

4 files changed

Lines changed: 41 additions & 39 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Add the following to your build.gradle file in the root of your project.
3333
...
3434
dependencies {
3535
...
36-
compile 'com.sendgrid:sendgrid-java:0.3.1'
36+
compile 'com.sendgrid:sendgrid-java:1.0.0'
3737
}
3838

3939
repositories {
@@ -131,11 +131,11 @@ email.setHtml("<h1>My first email through SendGrid");
131131
### Attachments
132132
133133
```java
134-
email.addAttachment("contents", "text.txt");
134+
email.addAttachment("text.txt", "contents");
135135
// or
136-
email.addAttachment(new File("./file.txt"), "text.txt");
136+
email.addAttachment("image.png", new File("./image.png"));
137137
// or
138-
email.addAttachment(new InputStream(new File("./file.txt")), "text.txt");
138+
email.addAttachment("text.txt", new InputStream(new File("./file.txt")));
139139
```
140140
141141
## [X-SMTPAPI](http://sendgrid.com/docs/API_Reference/SMTP_API/index.html)

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ apply plugin: 'maven'
1717
apply plugin: 'signing'
1818

1919
group = 'com.sendgrid'
20-
version = "0.3.1"
20+
version = "1.0.0"
2121
ext.packaging = 'jar'
2222

2323
if (!hasProperty("sonatypeUsername")) {

src/main/java/com/sendgrid/SendGrid.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static class Email {
7979
public Map headers = new HashMap();
8080

8181
public Email () {
82-
this.smtpapi = new SMTPAPI();
82+
this.smtpapi = new SMTPAPI();
8383
}
8484

8585
public Email addTo(String to) {
@@ -122,52 +122,53 @@ public Email setHtml(String html) {
122122
return this;
123123
}
124124

125-
public Email addSubstitution(String key, String[] val) {
126-
this.smtpapi.addSubstitutions(key, val);
127-
return this;
125+
public Email addSubstitution(String key, String[] val) {
126+
this.smtpapi.addSubstitutions(key, val);
127+
return this;
128128
}
129-
130-
public Email addUniqueArg(String key, String val) {
129+
130+
public Email addUniqueArg(String key, String val) {
131131
this.smtpapi.addUniqueArg(key, val);
132132
return this;
133133
}
134-
135-
public Email addCategory(String category) {
134+
135+
public Email addCategory(String category) {
136136
this.smtpapi.addCategory(category);
137137
return this;
138138
}
139-
140-
public Email addSection(String key, String val) {
141-
this.smtpapi.addSection(key, val);
139+
140+
public Email addSection(String key, String val) {
141+
this.smtpapi.addSection(key, val);
142142
return this;
143143
}
144-
144+
145145
public Email addFilter(String filter_name, String parameter_name, String parameter_value) {
146-
this.smtpapi.addFilter(filter_name, parameter_name, parameter_value);
147-
return this;
146+
this.smtpapi.addFilter(filter_name, parameter_name, parameter_value);
147+
return this;
148148
}
149149

150-
public Email addAttachment(File file, String name) throws FileNotFoundException {
151-
return this.addAttachment(new FileInputStream(file), name);
150+
public Email addAttachment(String name, File file) {
151+
this.attachments.put(name, file);
152+
return this;
152153
}
153154

154-
public Email addAttachment(String file, String name) {
155+
public Email addAttachment(String name, String file) {
155156
this.attachments.put(name, file);
156157
return this;
157158
}
158159

159-
public Email addAttachment(InputStream file, String name) {
160+
public Email addAttachment(String name, InputStream file) {
160161
Scanner scanner = new Scanner(file, "UTF-8");
161162
String buffer = new String();
162163
while (scanner.hasNextLine()) {
163164
buffer += scanner.nextLine();
164165
}
165166
scanner.close();
166-
return this.addAttachment(buffer, name);
167+
return this.addAttachment(name, buffer);
167168
}
168-
169-
public Email addHeader(String key, String val) {
170-
this.headers.put(key, val);
169+
170+
public Email addHeader(String key, String val) {
171+
this.headers.put(key, val);
171172
return this;
172173
}
173174

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

184185
if (this.from != null && !this.from.isEmpty()) {
185-
String value = this.from;
186+
String value = this.from;
186187
body.put(PARAM_FROM, value);
187188
}
188189

189190
if (this.fromname != null && !this.fromname.isEmpty()) {
190-
String value = this.fromname;
191+
String value = this.fromname;
191192
body.put(PARAM_FROMNAME, value);
192193
}
193194

@@ -217,14 +218,14 @@ public Map toWebFormat() {
217218
body.put(PARAM_HTML, value);
218219
}
219220

220-
if (!this.headers.isEmpty()) {
221+
if (!this.headers.isEmpty()) {
221222
JSONObject json_headers = new JSONObject(this.headers);
222223
String serialized_headers = json_headers.toString();
223224
body.put(PARAM_HEADERS, serialized_headers);
224-
}
225+
}
225226

226227
if (!this.smtpapi.jsonString().equals("{}")) {
227-
String value = this.smtpapi.jsonString();
228+
String value = this.smtpapi.jsonString();
228229
body.put(PARAM_XSMTPAPI, value);
229230
}
230231

src/test/java/com/sendgrid/SendGridTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class SendGridTest {
6767
correct.put("to", address);
6868

6969
assertEquals(correct, email.toWebFormat());
70-
}
70+
}
7171

7272
@Test public void testSetFromName() {
7373
email = new SendGrid.Email();
@@ -145,7 +145,7 @@ public class SendGridTest {
145145
email = new SendGrid.Email();
146146

147147
email.addHeader("key", "value");
148-
email.addHeader("other", "other-value");
148+
email.addHeader("other", "other-value");
149149

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

160160
File file = new File(getClass().getResource("/test.txt").getFile());
161-
email.addAttachment(file, "test.txt");
161+
email.addAttachment("test.txt", file);
162162

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

166166
assertEquals(correct, email.toWebFormat());
167167
}
@@ -176,11 +176,12 @@ public class SendGridTest {
176176
email.setText("Test body");
177177
email.setHtml("Test body");
178178
email.addCategory("-TEST-");
179-
File file = new File(getClass().getResource("/test.txt").getFile());
180-
email.addAttachment(file, "test.txt");
179+
File file = new File(getClass().getResource("/image.png").getFile());
180+
email.addAttachment("image.png", file);
181181
SendGrid sendgrid = new SendGrid(USERNAME, PASSWORD);
182182
SendGrid.Response resp = sendgrid.send(email);
183183

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

0 commit comments

Comments
 (0)