diff --git a/pom.xml b/pom.xml index 579ab75..8870a48 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,11 @@ jackson-databind 2.2.3 + + commons-io + commons-io + 2.0 + @@ -73,12 +78,6 @@ 1.8.4 test - - commons-io - commons-io - 2.0 - test - diff --git a/src/main/java/com/cribbstechnologies/clients/mandrill/model/MandrillAttachment.java b/src/main/java/com/cribbstechnologies/clients/mandrill/model/MandrillAttachment.java index 1e7c92c..0d5b6e6 100644 --- a/src/main/java/com/cribbstechnologies/clients/mandrill/model/MandrillAttachment.java +++ b/src/main/java/com/cribbstechnologies/clients/mandrill/model/MandrillAttachment.java @@ -1,5 +1,13 @@ package com.cribbstechnologies.clients.mandrill.model; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.IOUtils; + public class MandrillAttachment { String type; @@ -12,6 +20,28 @@ public MandrillAttachment(String type, String name, String content) { this.content = content; } + public MandrillAttachment(File file, String type) { + this.name = file.getName(); + this.type = type; + try { + FileInputStream is = new FileInputStream(file); + this.content = new String(Base64.encodeBase64(IOUtils.toByteArray(is))); + is.close(); + } catch (IOException e) { + throw new RuntimeException("Can't encode attachment file " + file); + } + } + + public MandrillAttachment(InputStream is, String fileName, String type) { + this.name = fileName; + this.type = type; + try { + this.content = new String(Base64.encodeBase64(IOUtils.toByteArray(is))); + } catch (IOException e) { + throw new RuntimeException("Can't encode attachment " + fileName); + } + } + public String getContent() { return this.content; } diff --git a/src/main/java/com/cribbstechnologies/clients/mandrill/model/MandrillRecipient.java b/src/main/java/com/cribbstechnologies/clients/mandrill/model/MandrillRecipient.java index f9842fe..52ef440 100644 --- a/src/main/java/com/cribbstechnologies/clients/mandrill/model/MandrillRecipient.java +++ b/src/main/java/com/cribbstechnologies/clients/mandrill/model/MandrillRecipient.java @@ -10,6 +10,10 @@ public class MandrillRecipient { String name; String type; + public MandrillRecipient(String email) { + this(email, email); + } + public MandrillRecipient(String name, String email) { this.email = email; this.name = name;