diff --git a/dist/JavaHelper.jar b/dist/JavaHelper.jar index c69c18a..46959b1 100644 Binary files a/dist/JavaHelper.jar and b/dist/JavaHelper.jar differ diff --git a/src/com/kentcdodds/javahelper/helpers/EmailHelper.java b/src/com/kentcdodds/javahelper/helpers/EmailHelper.java index 8b01892..f405dfb 100644 --- a/src/com/kentcdodds/javahelper/helpers/EmailHelper.java +++ b/src/com/kentcdodds/javahelper/helpers/EmailHelper.java @@ -216,6 +216,6 @@ public static EmailAttachment zipAttachments(String attachmentName, EmailAttachm files[i] = new HelperFile(fileBytes, emailAttachment.getAttachmentName()); } byte[] zippedBytes = IOHelper.zipFiles(files); - return new EmailAttachment(zippedBytes, "attachments.zip"); + return new EmailAttachment(zippedBytes, "attachments.zip", "application/zip", Message.ATTACHMENT); } } diff --git a/src/com/kentcdodds/javahelper/model/EmailAttachment.java b/src/com/kentcdodds/javahelper/model/EmailAttachment.java index 80db076..5c28326 100644 --- a/src/com/kentcdodds/javahelper/model/EmailAttachment.java +++ b/src/com/kentcdodds/javahelper/model/EmailAttachment.java @@ -9,6 +9,7 @@ import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; +import javax.activation.URLDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.internet.MimeBodyPart; @@ -28,11 +29,18 @@ public class EmailAttachment { private String extension; private String fullFileName; private MimeBodyPart bodyPart; + /** + * Used when generating the body part + */ + private String contentId, contentType, disposition; /** - * Don't forget to set filetype, filename, and file + * Don't forget to set filetype, filename, contentType (defaults to "application/octet-stream"), disposition (defaults + * to Message.ATTACHMENT", and file/url/filebytes. */ public EmailAttachment() { + contentType = "application/octet-stream"; + disposition = Message.ATTACHMENT; } /** @@ -41,9 +49,11 @@ public EmailAttachment() { * @param fileBytes * @param fullFileName example: "document.txt" */ - public EmailAttachment(byte[] fileBytes, String fullFileName) { + public EmailAttachment(byte[] fileBytes, String fullFileName, String contentType, String disposition) { this.fileBytes = fileBytes; this.fullFileName = fullFileName; + this.contentType = contentType; + this.disposition = disposition; } /** @@ -51,8 +61,10 @@ public EmailAttachment(byte[] fileBytes, String fullFileName) { * * @param file */ - public EmailAttachment(File file) { + public EmailAttachment(File file, String contentType, String disposition) { this.file = file; + this.contentType = contentType; + this.disposition = disposition; } /** @@ -60,8 +72,10 @@ public EmailAttachment(File file) { * * @param file */ - public EmailAttachment(URL url) { + public EmailAttachment(URL url, String contentType, String disposition) { this.url = url; + this.contentType = contentType; + this.disposition = disposition; } /** @@ -79,44 +93,84 @@ public boolean generateMimeBodyPart() throws MessagingException, FileNotFoundExc if (bodyPart != null) { return true; } + bodyPart = new MimeBodyPart(); if (file != null) { - source = new FileDataSource(file) { - - @Override - public String getContentType() { - return "application/octet-stream"; - } - }; - } else if (fileBytes != null || url != null) { - if (url != null) { - generateFileBytes(); //download the file into the fileBytes array. - } - source = new ByteArrayDataSource(fileBytes, Message.ATTACHMENT) { - - @Override - public String getContentType() { - return "application/octet-stream"; - } - }; + source = getFileBodyPart(); + bodyPart.setDisposition(disposition); + } else if (fileBytes != null) { + source = getByteArrayBodyPart(); + } else if (url != null) { + source = getURLBodyPart(); + bodyPart.setDisposition(disposition); } else { + bodyPart = null; //Because it was set to a new one before but it doesn't have a datahandler. return false; } - bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(source)); bodyPart.setFileName(getAttachmentName()); + if (contentId != null) { + bodyPart.setContentID(contentId); + } return true; } /** - * Sets the attachment as an inline image. + * Returns a new FileDataSource with the file + * + * @return + */ + private DataSource getFileBodyPart() { + return new FileDataSource(file) { + + @Override + public String getContentType() { + return contentType; + } + }; + + } + + /** + * Returns a new ByteArrayDataSource with the filebytes and disposition + * + * @return + */ + private DataSource getByteArrayBodyPart() { + return new ByteArrayDataSource(fileBytes, disposition) { + + @Override + public String getContentType() { + return contentType; + } + }; + } + + /** + * Returns a new URLDataSource with the url. + * + * @return + */ + private DataSource getURLBodyPart() { + return new URLDataSource(url) { + + @Override + public String getContentType() { + return contentType; + } + }; + } + + /** + * Sets the attachment as an inline attachment. * + * @param contentType the type of content ("image/jpeg" for example). * @param contentId * @throws MessagingException */ - public void setBodyPartAsInlineResource(String contentType, String contentId) throws MessagingException { - bodyPart.setHeader("Content-Type", contentType); - bodyPart.setDisposition(MimeBodyPart.INLINE); - bodyPart.setContentID("<" + contentId + ">"); + public void setBodyPartAsInlineAttachment(String contentType, String contentId) throws MessagingException { + setContentType(contentType); + setDisposition(MimeBodyPart.INLINE); + setContentId("<" + contentId + ">"); } /** @@ -171,6 +225,8 @@ public String getFullFileName() { if (fullFileName == null) { if (file != null) { fullFileName = file.getName(); + } else if (url != null) { + fullFileName = url.getFile().substring(1); } else { fullFileName = getFileName() + "." + getExtension(); } @@ -214,20 +270,19 @@ public void generateFileBytes() throws FileNotFoundException, IOException, Excep } } - @Override public String toString() { - return StringHelper.splitBy(StringHelper.newline - , "Attachment Name: " + ((attachmentName != null) ? attachmentName : "null") - , "File: " + ((file != null) ? file.getPath() : "null") - , "File Name: " + ((fileName != null) ? fileName : "null") - , "Extension: " + ((extension != null) ? extension : "null") - , "Full File Name: " + ((fullFileName != null) ? fullFileName : "null") - , "URL: " + ((url != null) ? url : "null") - , "File Bytes size: " + ((fileBytes != null) ? fileBytes.length : "null") - , "Mime Body Part: " + ((bodyPart != null) ? "exists" : "null") - ); + return StringHelper.splitBy( + StringHelper.newline, "Attachment Name: " + ((attachmentName != null) ? attachmentName : "null"), + "File: " + ((file != null) ? file.getPath() : "null"), + "File Name: " + ((fileName != null) ? fileName : "null"), + "Extension: " + ((extension != null) ? extension : "null"), + "Full File Name: " + ((fullFileName != null) ? fullFileName : "null"), + "URL: " + ((url != null) ? url : "null"), + "File Bytes size: " + ((fileBytes != null) ? fileBytes.length : "null"), + "Mime Body Part: " + ((bodyPart != null) ? "exists" : "null")); } + /** * @return the file */ @@ -300,4 +355,26 @@ public URL getUrl() { public void setUrl(URL url) { this.url = url; } + + /** + * @param contentId the contentId to set. Note: If this is to be a disposition of MimeBodyPart.INLINE, surround the + * contentId with "<" + contentId + ">". This is something special with inline attachments. + */ + public void setContentId(String contentId) { + this.contentId = contentId; + } + + /** + * @param contentType the contentType to set + */ + public void setContentType(String contentType) { + this.contentType = contentType; + } + + /** + * @param disposition the disposition to set + */ + public void setDisposition(String disposition) { + this.disposition = disposition; + } } diff --git a/src/com/kentcdodds/javahelper/test/TestArchive.java b/src/com/kentcdodds/javahelper/test/TestArchive.java index 6b19211..606d8c2 100644 --- a/src/com/kentcdodds/javahelper/test/TestArchive.java +++ b/src/com/kentcdodds/javahelper/test/TestArchive.java @@ -16,7 +16,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.imageio.ImageIO; +import javax.mail.Message; import javax.mail.MessagingException; +import javax.mail.Part; import javax.mail.Session; import javax.swing.ImageIcon; import javax.swing.JDialog; @@ -240,8 +242,9 @@ public static void zip() throws FileNotFoundException, IOException, Exception { } public static void email() throws Exception { - String user = gmailUser; - String password = gmailPassword; +// String user = gmailUser; + String user = ldsUser; +// String password = gmailPassword; String from = user; String contentId = "image2"; List to = new ArrayList<>(); @@ -251,20 +254,29 @@ public static void email() throws Exception { List bcc = new ArrayList<>(); // bcc.add("gfdjakl@mailinator.com"); String subject = "This is a test subject!" + new Random().nextInt(1000); - String body = "
This is text before
\"Inline

This is after
"; + String body = "
This is text before
\"Inline

This is after
"; Email email = new Email(from, to, cc, bcc, subject, body); email.setHtml(true); - EmailAttachment attachment = new EmailAttachment(); -// attachment.setFile(new File("C:\\Users\\kentcdodds\\Documents\\test attachment.txt")); -// attachment.setFile(new File("C:\\Users\\kentcdodds\\Downloads\\smileyfacerd1.jpg")); - attachment.setFile(new File("C:\\Users\\kentcdodds\\Downloads\\CrystalReportViewer.pdf")); -// attachment.setFile(new File(ioPlaygroundDir, "I am a print test.txt")); - attachment.generateMimeBodyPart(); - attachment.setBodyPartAsInlineResource("application/pdf", contentId); - email.addEmailAttachment(attachment); + EmailAttachment attachment1 = new EmailAttachment(); +// attachment1.setFile(new File("C:\\Users\\kentcdodds\\Documents\\test attachment.txt")); +// attachment1.setFile(new File("C:\\Users\\kentcdodds\\Downloads\\smileyfacerd1.jpg")); + attachment1.setFile(new File("C:\\Users\\kentcdodds\\Downloads\\CrystalReportViewer.pdf")); +// attachment1.setFile(new File(ioPlaygroundDir, "I am a print test.txt")); + attachment1.setContentType("application/pdf"); + attachment1.generateMimeBodyPart(); + + EmailAttachment attachment2 = new EmailAttachment(new URL("http://www.kentcdodds.com/photo.jpg"), "image/jpeg", Message.INLINE); + attachment2.setContentId("<" + contentId + ">"); + attachment2.generateMimeBodyPart(); + + EmailAttachment attachment3 = new EmailAttachment("This is a test".getBytes(), "Test.txt", "text/plain", Message.ATTACHMENT); + attachment3.generateMimeBodyPart(); + + email.addEmailAttachments(attachment1, attachment2, attachment3); + email.addReplyTo("kentdoddsproductions@gmail.com", "kentcdodds@gmail.com"); -// Session session = Session.getInstance(mailServerProperties, null); - Session session = EmailHelper.getGoogleSession(user, password); + Session session = Session.getInstance(mailServerProperties, null); +// Session session = EmailHelper.getGoogleSession(user, password); session.setDebug(true); EmailHelper.sendEmail(session, email); // System.out.println(ReflectionHelper.getObjectInString(email, true, 1, true, 1)); diff --git a/store/JavaHelper.jar b/store/JavaHelper.jar index 108f462..62111f4 100644 Binary files a/store/JavaHelper.jar and b/store/JavaHelper.jar differ