001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.mail;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022:
023: import org.apache.commons.mail.mocks.MockHtmlEmailConcrete;
024: import org.apache.commons.mail.settings.EmailConfiguration;
025:
026: /**
027: * JUnit test case verifying bugzilla issue 30973 is fixed.
028: *
029: * @since 1.0
030: * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a>
031: * @version $Id: SendWithAttachmentsTest.java 279300 2005-09-07 11:43:52Z henning $
032: */
033:
034: public class SendWithAttachmentsTest extends BaseEmailTestCase {
035: /** */
036: private MockHtmlEmailConcrete email = null;
037:
038: /**
039: * @param name name
040: */
041: public SendWithAttachmentsTest(String name) {
042: super (name);
043: }
044:
045: /** */
046: protected void setUp() {
047: super .setUp();
048: // reusable objects to be used across multiple tests
049: this .email = new MockHtmlEmailConcrete();
050: }
051:
052: /** */
053: public void testSendNoAttachments() {
054: try {
055: this .getMailServer();
056:
057: String strSubject = "Test HTML Send #1 Subject (w charset)";
058:
059: this .email = new MockHtmlEmailConcrete();
060: this .email.setHostName(this .strTestMailServer);
061: this .email.setSmtpPort(this .getMailServerPort());
062: this .email.setFrom(this .strTestMailFrom);
063: this .email.addTo(this .strTestMailTo);
064:
065: this .email.setAuthentication(this .strTestUser,
066: this .strTestPasswd);
067:
068: this .email.setCharset(Email.ISO_8859_1);
069: this .email.setSubject(strSubject);
070:
071: URL url = new URL(EmailConfiguration.TEST_URL);
072: String cid = this .email.embed(url, "Apache Logo");
073:
074: String strHtmlMsg = "<html>The Apache logo - <img src=\"cid:"
075: + cid + "\"><html>";
076:
077: this .email.setHtmlMsg(strHtmlMsg);
078: this .email
079: .setTextMsg("Your email client does not support HTML emails");
080:
081: this .email.send();
082: this .fakeMailServer.stop();
083:
084: // validate txt message
085: validateSend(this .fakeMailServer, strSubject, this .email
086: .getTextMsg(), this .email.getFromAddress(),
087: this .email.getToList(), this .email.getCcList(),
088: this .email.getBccList(), true);
089:
090: // validate html message
091: validateSend(this .fakeMailServer, strSubject, this .email
092: .getHtmlMsg(), this .email.getFromAddress(),
093: this .email.getToList(), this .email.getCcList(),
094: this .email.getBccList(), false);
095: }
096:
097: catch (MalformedURLException e) {
098: e.printStackTrace();
099: fail("Unexpected exception thrown");
100: } catch (IOException e) {
101: e.printStackTrace();
102: fail("Failed to save email to output file");
103: } catch (Exception e) {
104: e.printStackTrace();
105: fail("Unexpected exception thrown");
106: }
107:
108: }
109:
110: /** */
111: public void testSendWAttachments() {
112: EmailAttachment attachment = new EmailAttachment();
113: File testFile = null;
114:
115: try {
116: /** File to used to test file attachmetns (Must be valid) */
117: testFile = File.createTempFile("commons-email-testfile",
118: ".txt");
119: } catch (IOException e) {
120: e.printStackTrace();
121: fail("Test file cannot be found");
122: }
123:
124: // ====================================================================
125: // Test Success
126: // ====================================================================
127: try {
128: this .getMailServer();
129:
130: String strSubject = "Test HTML Send #1 Subject (w charset)";
131:
132: this .email = new MockHtmlEmailConcrete();
133: this .email.setHostName(this .strTestMailServer);
134: this .email.setSmtpPort(this .getMailServerPort());
135: this .email.setFrom(this .strTestMailFrom);
136: this .email.addTo(this .strTestMailTo);
137:
138: /** File to used to test file attachmetns (Must be valid) */
139: attachment.setName("Test Attachment");
140: attachment.setDescription("Test Attachment Desc");
141: attachment.setPath(testFile.getAbsolutePath());
142: this .email.attach(attachment);
143:
144: this .email.setAuthentication(this .strTestUser,
145: this .strTestPasswd);
146:
147: this .email.setCharset(Email.ISO_8859_1);
148: this .email.setSubject(strSubject);
149:
150: URL url = new URL(EmailConfiguration.TEST_URL);
151: String cid = this .email.embed(url, "Apache Logo");
152:
153: String strHtmlMsg = "<html>The Apache logo - <img src=\"cid:"
154: + cid + "\"><html>";
155:
156: this .email.setHtmlMsg(strHtmlMsg);
157: this .email
158: .setTextMsg("Your email client does not support HTML emails");
159:
160: this .email.send();
161: this .fakeMailServer.stop();
162: // validate txt message
163: validateSend(this .fakeMailServer, strSubject, this .email
164: .getTextMsg(), this .email.getFromAddress(),
165: this .email.getToList(), this .email.getCcList(),
166: this .email.getBccList(), true);
167:
168: // validate html message
169: validateSend(this .fakeMailServer, strSubject, this .email
170: .getHtmlMsg(), this .email.getFromAddress(),
171: this .email.getToList(), this .email.getCcList(),
172: this .email.getBccList(), false);
173:
174: // validate attachment
175: validateSend(this .fakeMailServer, strSubject, attachment
176: .getName(), this .email.getFromAddress(), this .email
177: .getToList(), this .email.getCcList(), this .email
178: .getBccList(), false);
179: }
180:
181: catch (MalformedURLException e) {
182: e.printStackTrace();
183: fail("Unexpected exception thrown");
184: } catch (IOException e) {
185: e.printStackTrace();
186: fail("Failed to save email to output file");
187: } catch (Exception e) {
188: e.printStackTrace();
189: fail("Unexpected exception thrown");
190: }
191: }
192:
193: }
|