commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sgoes...@apache.org
Subject svn commit: r1050180 - in /commons/proper/email/trunk/src: java/org/apache/commons/mail/MultiPartEmail.java test/org/apache/commons/mail/EmailLiveTest.java test/org/apache/commons/mail/MultiPartEmailTest.java
Date Thu, 16 Dec 2010 21:50:57 GMT
Author: sgoeschl
Date: Thu Dec 16 21:50:57 2010
New Revision: 1050180

URL: http://svn.apache.org/viewvc?rev=1050180&view=rev
Log:
Adding a MultiPartEmail.attach(File) method

Modified:
    commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java
    commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java
    commons/proper/email/trunk/src/test/org/apache/commons/mail/MultiPartEmailTest.java

Modified: commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java?rev=1050180&r1=1050179&r2=1050180&view=diff
==============================================================================
--- commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java (original)
+++ commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java Thu Dec
16 21:50:57 2010
@@ -264,6 +264,39 @@ public class MultiPartEmail extends Emai
     }
 
     /**
+     * Attach a file
+     *
+     * @param file A file attachment
+     * @return A MultiPartEmail.
+     * @throws EmailException see javax.mail.internet.MimeBodyPart
+     *  for definitions
+     * @since 1.3
+     */
+    public MultiPartEmail attach(File file)
+        throws EmailException
+    {
+        String fileName = file.getAbsolutePath();
+
+        try
+        {
+            if (!file.exists())
+            {
+                throw new IOException("\"" + fileName + "\" does not exist");
+            }
+
+            FileDataSource fds = new FileDataSource(file);
+
+            return attach(fds, file.getName(), null, EmailAttachment.ATTACHMENT);
+        }
+        catch (IOException e)
+        {
+            throw new EmailException(
+                "Cannot attach file \"" + fileName + "\"",
+                e);
+        }
+    }
+
+    /**
      * Attach an EmailAttachment.
      *
      * @param attachment An EmailAttachment.

Modified: commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java?rev=1050180&r1=1050179&r2=1050180&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java (original)
+++ commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java Thu Dec
16 21:50:57 2010
@@ -124,11 +124,26 @@ public class EmailLiveTest extends BaseE
     public void testSimpleEmail() throws Exception
     {
         SimpleEmail email = (SimpleEmail) create(SimpleEmail.class);
-        email.setSubject("TestMail");
+        email.setSubject("TestSimpleMail");
         email.setMsg("This is a test mail ... :-)");
 
         EmailUtils.writeMimeMessage( new File("./target/test-emails/simplemail.eml"), send(email).getMimeMessage());
     }
+
+    /**
+     * A sanity check that a simple email also works in reality.
+     *
+     * @throws Exception the test failed
+     */
+    public void testMultiPartEmail() throws Exception
+    {
+        MultiPartEmail email = (MultiPartEmail) create(MultiPartEmail.class);
+        email.setSubject("TestMultiPartMail");
+        email.setMsg("This is a test mail ... :-)");
+        email.attach(new File("./src/test/attachments/logo.pdf"));
+
+        EmailUtils.writeMimeMessage( new File("./target/test-emails/multipart.eml"), send(email).getMimeMessage());
+    }
     
     /**
      * This test checks the various options of building a HTML email.

Modified: commons/proper/email/trunk/src/test/org/apache/commons/mail/MultiPartEmailTest.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/MultiPartEmailTest.java?rev=1050180&r1=1050179&r2=1050180&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/org/apache/commons/mail/MultiPartEmailTest.java (original)
+++ commons/proper/email/trunk/src/test/org/apache/commons/mail/MultiPartEmailTest.java Thu
Dec 16 21:50:57 2010
@@ -198,7 +198,7 @@ public class MultiPartEmailTest extends 
         EmailAttachment attachment;
 
         // ====================================================================
-        // Test Success - File
+        // Test Success - EmailAttachment
         // ====================================================================
         attachment = new EmailAttachment();
         attachment.setName("Test Attachment");
@@ -217,12 +217,18 @@ public class MultiPartEmailTest extends 
         this.email.attach(attachment);
 
         // ====================================================================
+        // Test Success - File
+        // ====================================================================
+        this.email.attach(testFile);
+        assertTrue(this.email.isBoolHasAttachments());
+
+        // ====================================================================
         // Test Exceptions
         // ====================================================================
         // null attachment
         try
         {
-            this.email.attach(null);
+            this.email.attach((EmailAttachment) null);
             fail("Should have thrown an exception");
         }
         catch (EmailException e)



Mime
View raw message