Skip to content

Commit

Permalink
Added method to send an email with attachment given as path.
Browse files Browse the repository at this point in the history
  • Loading branch information
rw42 committed Oct 9, 2012
1 parent 9c8e1dc commit 7eacd1c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions ivy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<dependency org="commons-io" name="commons-io" rev="2.4" />
<dependency org="commons-fileupload" name="commons-fileupload"
rev="1.2.2" />
<dependency org="org.apache.commons" name="commons-email" rev="1.2"/>
<dependency org="org.freemarker" name="freemarker" rev="2.3.19" />
<dependency org="org.opensymphony.quartz" name="quartz"
rev="1.6.1" />
Expand Down
7 changes: 6 additions & 1 deletion pom_minimal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.2</version>
</dependency>

<!--
<dependency>
<groupId>log4j</groupId>
Expand Down
3 changes: 2 additions & 1 deletion src/org/molgenis/util/EmailService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.molgenis.util;

import java.io.ByteArrayOutputStream;
import java.io.File;

import org.molgenis.util.SimpleEmailService.EmailException;

Expand All @@ -27,6 +26,8 @@ public interface EmailService

public boolean email(String subject, String body, String toEmail, boolean deObf, String sender) throws EmailException;

public boolean email(String subject, String body, String toEmail, String fileAttachment, boolean deObf) throws EmailException;

public boolean email(String subject, String body, String toEmail, String fileAttachment, ByteArrayOutputStream outputStream, boolean deObf) throws EmailException;

public boolean email(String subject, String body, String toEmail, String fileAttachment, ByteArrayOutputStream outputStream, boolean deObf, String sender) throws EmailException;
Expand Down
40 changes: 36 additions & 4 deletions src/org/molgenis/util/SimpleEmailService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.molgenis.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
Expand All @@ -18,6 +14,9 @@
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;


public class SimpleEmailService implements EmailService
{
Expand Down Expand Up @@ -97,6 +96,39 @@ public boolean email(String subject, String body, String toEmail, boolean deObf,
}
}

public boolean email(String subject, String body, String toEmail, String fileAttachment, boolean deObf) throws EmailException
{
try
{
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(fileAttachment);
attachment.setDisposition(EmailAttachment.ATTACHMENT);

// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName(this.smtpHostname);
email.addTo(toEmail);
email.setFrom(this.smtpFromAddress);
email.setSubject(subject);
email.setMsg(body);
email.setAuthentication(this.smtpUser, (deObf ? HtmlTools.fromSafeUrlStringO_b_f(smtpAu) : smtpAu));

// add the attachment
email.attach(attachment);

// send the email
email.send();

return true;
}
catch (Exception e)
{
e.printStackTrace();
throw new EmailException(e);
}
}

public boolean email(String subject, String body, String toEmail, String fileAttachment, ByteArrayOutputStream outputStream, boolean deObf) throws EmailException
{
String sender = "MOLGENIS";
Expand Down

0 comments on commit 7eacd1c

Please sign in to comment.