Skip to content

Commit

Permalink
Added ability to attach file from URL to an e-mail. Fixed the EmailAt…
Browse files Browse the repository at this point in the history
…tachment process
  • Loading branch information
kentcdodds authored and kentcdodds committed Jun 21, 2012
1 parent 0c20736 commit 65798c6
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 53 deletions.
Binary file modified dist/JavaHelper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/com/kentcdodds/javahelper/helpers/EmailHelper.java
Expand Up @@ -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);
}
}
155 changes: 116 additions & 39 deletions src/com/kentcdodds/javahelper/model/EmailAttachment.java
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -41,27 +49,33 @@ 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;
}

/**
* Constructor: This is the minimum you need to send the attachment. The file must be valid
*
* @param file
*/
public EmailAttachment(File file) {
public EmailAttachment(File file, String contentType, String disposition) {
this.file = file;
this.contentType = contentType;
this.disposition = disposition;
}

/**
* Constructor: This is the minimum you need to send the attachment. The url must be valid
*
* @param file
*/
public EmailAttachment(URL url) {
public EmailAttachment(URL url, String contentType, String disposition) {
this.url = url;
this.contentType = contentType;
this.disposition = disposition;
}

/**
Expand All @@ -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 + ">");
}

/**
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
38 changes: 25 additions & 13 deletions src/com/kentcdodds/javahelper/test/TestArchive.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> to = new ArrayList<>();
Expand All @@ -251,20 +254,29 @@ public static void email() throws Exception {
List<String> bcc = new ArrayList<>();
// bcc.add("gfdjakl@mailinator.com");
String subject = "This is a test subject!" + new Random().nextInt(1000);
String body = "<div>This is text before</div><img src=\"cid:" + contentId + "\" alt=\"Inline image 1\"><div><br></div><div>This is after</div>";
String body = "<div>This is text before</div><img src=\"cid:" + contentId + "\" alt=\"Inline image 1\" width=\"150\"><div><br></div><div>This is after</div>";
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));
Expand Down
Binary file modified store/JavaHelper.jar
Binary file not shown.

0 comments on commit 65798c6

Please sign in to comment.