Author: dion Date: Wed Jan 31 21:58:39 2007 New Revision: 502132 URL: http://svn.apache.org/viewvc?view=rev&rev=502132 Log: Applied EMAIL-59. Also changed the way the HTMLEmailTest class did it's failure handling, because failing tests would just print out a message and not provide the actual exception message and a stack trace to help fix it.
Modified: jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java Modified: jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java?view=diff&rev=502132&r1=502131&r2=502132 ============================================================================== --- jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java (original) +++ jakarta/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java Wed Jan 31 21:58:39 2007 @@ -97,6 +97,12 @@ */ public static final String MAIL_TRANSPORT_TLS = "mail.smtp.starttls.enable"; /** */ + public static final String MAIL_SMTP_SOCKET_FACTORY_FALLBACK = "mail.smtp.socketFactory.fallback"; + /** */ + public static final String MAIL_SMTP_SOCKET_FACTORY_CLASS = "mail.smtp.socketFactory.class"; + /** */ + public static final String MAIL_SMTP_SOCKET_FACTORY_PORT = "mail.smtp.socketFactory.port"; + /** */ public static final String SMTP = "smtp"; /** */ public static final String TEXT_HTML = "text/html"; @@ -160,6 +166,12 @@ * Defaults to the standard port ( 25 ). */ protected String smtpPort = "25"; + + /** + * The port number of the SSL enabled SMTP server; + * defaults to the standard port, 465. + */ + protected String sslSmtpPort = "465"; /** List of "to" email adresses */ protected List toList = new ArrayList(); @@ -206,6 +218,8 @@ /** does server require TLS encryption for authentication */ protected boolean tls = false; + /** does the current transport use SSL encryption? */ + protected boolean ssl = false; /** * Setting to true will enable the display of debug information. @@ -442,6 +456,14 @@ properties.setProperty(MAIL_SMTP_AUTH, "true"); } + if (this.ssl) + { + properties.setProperty(MAIL_PORT, sslSmtpPort); + properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_PORT, sslSmtpPort); + properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_CLASS, "javax.net.ssl.SSLSocketFactory"); + properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_FALLBACK, "false"); + } + if (this.bounceAddress != null) { properties.setProperty(MAIL_SMTP_FROM, this.bounceAddress); @@ -1114,6 +1136,46 @@ this.popHost = newPopHost; this.popUsername = newPopUsername; this.popPassword = newPopPassword; + } + + /** + * Returns whether SSL encryption for the transport is currently enabled. + * @return true if SSL enabled for the transport + */ + public boolean isSSL() { + return ssl; + } + + /** + * Sets whether SSL encryption should be enabled for the SMTP transport. + * @param ssl whether to enable the SSL transport + */ + public void setSSL(boolean ssl) { + this.ssl = ssl; + } + + /** + * Returns the current SSL port used by the SMTP transport. + * @return the current SSL port used by the SMTP transport + */ + public String getSslSmtpPort() { + if (EmailUtils.isNotEmpty(this.sslSmtpPort)) + { + return this.sslSmtpPort; + } + else + { + return this.session.getProperty(MAIL_SMTP_SOCKET_FACTORY_PORT); + } + } + + /** + * Sets the SSL port to use for the SMTP transport. Defaults to the standard + * port, 465. + * @param sslSmtpPort the SSL port to use for the SMTP transport + */ + public void setSslSmtpPort(String sslSmtpPort) { + this.sslSmtpPort = sslSmtpPort; } } Modified: jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java?view=diff&rev=502132&r1=502131&r2=502132 ============================================================================== --- jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java (original) +++ jakarta/commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java Wed Jan 31 21:58:39 2007 @@ -52,23 +52,17 @@ this.email = new MockHtmlEmailConcrete(); } - /** */ - public void testGetSetTextMsg() + /** + * @throws EmailException */ + public void testGetSetTextMsg() throws EmailException { // ==================================================================== // Test Success // ==================================================================== - try - { - for (int i = 0; i < testCharsValid.length; i++) - { - this.email.setTextMsg(testCharsValid[i]); - assertEquals(testCharsValid[i], this.email.getTextMsg()); - } - } - catch (EmailException e) + for (int i = 0; i < testCharsValid.length; i++) { - fail("Shoudn't have thrown exception"); + this.email.setTextMsg(testCharsValid[i]); + assertEquals(testCharsValid[i], this.email.getTextMsg()); } // ==================================================================== @@ -85,31 +79,22 @@ { assertTrue(true); } - catch (Exception e) - { - fail("Unexpected exception thrown"); - } } } - /** */ - public void testGetSetHtmlMsg() + /** + * @throws EmailException + */ + public void testGetSetHtmlMsg() throws EmailException { // ==================================================================== // Test Success // ==================================================================== - try - { - for (int i = 0; i < testCharsValid.length; i++) - { - this.email.setHtmlMsg(testCharsValid[i]); - assertEquals(testCharsValid[i], this.email.getHtmlMsg()); - } - } - catch (EmailException e) + for (int i = 0; i < testCharsValid.length; i++) { - fail("Shoudn't have thrown exception"); + this.email.setHtmlMsg(testCharsValid[i]); + assertEquals(testCharsValid[i], this.email.getHtmlMsg()); } // ==================================================================== @@ -126,34 +111,24 @@ { assertTrue(true); } - catch (Exception e) - { - fail("Unexpected exception thrown"); - } } } - /** */ - public void testGetSetMsg() + /** + * @throws EmailException */ + public void testGetSetMsg() throws EmailException { // ==================================================================== // Test Success // ==================================================================== - try + for (int i = 0; i < testCharsValid.length; i++) { - for (int i = 0; i < testCharsValid.length; i++) - { - this.email.setMsg(testCharsValid[i]); - assertEquals(testCharsValid[i], this.email.getTextMsg()); + this.email.setMsg(testCharsValid[i]); + assertEquals(testCharsValid[i], this.email.getTextMsg()); - assertTrue( - this.email.getHtmlMsg().indexOf(testCharsValid[i]) != -1); - } - } - catch (EmailException e) - { - fail("Shoudn't have thrown exception"); + assertTrue( + this.email.getHtmlMsg().indexOf(testCharsValid[i]) != -1); } // ==================================================================== @@ -170,10 +145,6 @@ { assertTrue(true); } - catch (Exception e) - { - fail("Unexpected exception thrown"); - } } } @@ -206,148 +177,119 @@ { assertTrue(true); } - catch (Exception e) - { - fail("Unexpected exception thrown"); - } } - /** */ - public void testSend() + /** + * @throws EmailException + * @throws IOException */ + public void testSend() throws EmailException, IOException { EmailAttachment attachment = new EmailAttachment(); File testFile = null; - try - { - /** File to used to test file attachments (Must be valid) */ - testFile = File.createTempFile("commons-email-testfile", ".txt"); - } - catch (IOException e) - { - fail("Test file cannot be found"); - } + /** File to used to test file attachments (Must be valid) */ + testFile = File.createTempFile("commons-email-testfile", ".txt"); // ==================================================================== // Test Success // ==================================================================== - try - { - this.getMailServer(); - - String strSubject = "Test HTML Send #1 Subject (w charset)"; + this.getMailServer(); - this.email = new MockHtmlEmailConcrete(); - this.email.setHostName(this.strTestMailServer); - this.email.setSmtpPort(this.getMailServerPort()); - this.email.setFrom(this.strTestMailFrom); - this.email.addTo(this.strTestMailTo); - - /** File to used to test file attachmetns (Must be valid) */ - attachment.setName("Test Attachment"); - attachment.setDescription("Test Attachment Desc"); - attachment.setPath(testFile.getAbsolutePath()); - this.email.attach(attachment); - - this.email.setAuthentication(this.strTestUser, this.strTestPasswd); - - this.email.setCharset(Email.ISO_8859_1); - this.email.setSubject(strSubject); - - URL url = new URL(EmailConfiguration.TEST_URL); - String cid = this.email.embed(url, "Apache Logo"); - - String strHtmlMsg = - "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>"; - - this.email.setHtmlMsg(strHtmlMsg); - this.email.setTextMsg( - "Your email client does not support HTML emails"); - - this.email.send(); - this.fakeMailServer.stop(); - // validate txt message - validateSend( - this.fakeMailServer, - strSubject, - this.email.getTextMsg(), - this.email.getFromAddress(), - this.email.getToList(), - this.email.getCcList(), - this.email.getBccList(), - true); - - // validate html message - validateSend( - this.fakeMailServer, - strSubject, - this.email.getHtmlMsg(), - this.email.getFromAddress(), - this.email.getToList(), - this.email.getCcList(), - this.email.getBccList(), - false); - - // validate attachment - validateSend( - this.fakeMailServer, - strSubject, - attachment.getName(), - this.email.getFromAddress(), - this.email.getToList(), - this.email.getCcList(), - this.email.getBccList(), - false); - } - catch (Exception e) - { - fail("Unexpected exception thrown"); - } + String strSubject = "Test HTML Send #1 Subject (w charset)"; - try - { - this.getMailServer(); - - this.email = new MockHtmlEmailConcrete(); - this.email.setHostName(this.strTestMailServer); - this.email.setSmtpPort(this.getMailServerPort()); - this.email.setFrom(this.strTestMailFrom); - this.email.addTo(this.strTestMailTo); - - if (this.strTestUser != null && this.strTestPasswd != null) - { - this.email.setAuthentication( - this.strTestUser, - this.strTestPasswd); - } - - String strSubject = "Test HTML Send #1 Subject (wo charset)"; - this.email.setSubject(strSubject); - this.email.setTextMsg("Test message"); - - this.email.send(); - this.fakeMailServer.stop(); - // validate txt message - validateSend( - this.fakeMailServer, - strSubject, - this.email.getTextMsg(), - this.email.getFromAddress(), - this.email.getToList(), - this.email.getCcList(), - this.email.getBccList(), - true); - } - - catch (IOException e) - { - fail("Failed to save email to output file"); - } - catch (Exception e) - { - e.printStackTrace(); - fail("Unexpected exception thrown"); - } + this.email = new MockHtmlEmailConcrete(); + this.email.setHostName(this.strTestMailServer); + this.email.setSmtpPort(this.getMailServerPort()); + this.email.setFrom(this.strTestMailFrom); + this.email.addTo(this.strTestMailTo); + + /** File to used to test file attachmetns (Must be valid) */ + attachment.setName("Test Attachment"); + attachment.setDescription("Test Attachment Desc"); + attachment.setPath(testFile.getAbsolutePath()); + this.email.attach(attachment); + + this.email.setAuthentication(this.strTestUser, this.strTestPasswd); + + this.email.setCharset(Email.ISO_8859_1); + this.email.setSubject(strSubject); + + URL url = new URL(EmailConfiguration.TEST_URL); + String cid = this.email.embed(url, "Apache Logo"); + + String strHtmlMsg = + "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>"; + + this.email.setHtmlMsg(strHtmlMsg); + this.email.setTextMsg( + "Your email client does not support HTML emails"); + + this.email.send(); + this.fakeMailServer.stop(); + // validate txt message + validateSend( + this.fakeMailServer, + strSubject, + this.email.getTextMsg(), + this.email.getFromAddress(), + this.email.getToList(), + this.email.getCcList(), + this.email.getBccList(), + true); + + // validate html message + validateSend( + this.fakeMailServer, + strSubject, + this.email.getHtmlMsg(), + this.email.getFromAddress(), + this.email.getToList(), + this.email.getCcList(), + this.email.getBccList(), + false); + + // validate attachment + validateSend( + this.fakeMailServer, + strSubject, + attachment.getName(), + this.email.getFromAddress(), + this.email.getToList(), + this.email.getCcList(), + this.email.getBccList(), + false); + + this.getMailServer(); + + this.email = new MockHtmlEmailConcrete(); + this.email.setHostName(this.strTestMailServer); + this.email.setSmtpPort(this.getMailServerPort()); + this.email.setFrom(this.strTestMailFrom); + this.email.addTo(this.strTestMailTo); + + if (this.strTestUser != null && this.strTestPasswd != null) + { + this.email.setAuthentication( + this.strTestUser, + this.strTestPasswd); + } + + strSubject = "Test HTML Send #1 Subject (wo charset)"; + this.email.setSubject(strSubject); + this.email.setTextMsg("Test message"); + + this.email.send(); + this.fakeMailServer.stop(); + // validate txt message + validateSend( + this.fakeMailServer, + strSubject, + this.email.getTextMsg(), + this.email.getFromAddress(), + this.email.getToList(), + this.email.getCcList(), + this.email.getBccList(), + true); } /** --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]