001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.mail; 018 019 import java.io.File; 020 import java.io.IOException; 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 512628 2007-02-28 06:56:28Z dion $ 032 */ 033 034 public class SendWithAttachmentsTest extends BaseEmailTestCase 035 { 036 /** */ 037 private MockHtmlEmailConcrete email; 038 039 /** 040 * @param name name 041 */ 042 public SendWithAttachmentsTest(String name) 043 { 044 super(name); 045 } 046 047 /** 048 * @throws Exception */ 049 protected void setUp() throws Exception 050 { 051 super.setUp(); 052 // reusable objects to be used across multiple tests 053 this.email = new MockHtmlEmailConcrete(); 054 } 055 056 /** 057 * @throws EmailException on an email error 058 * @throws IOException when sending fails, or a bad URL is used 059 */ 060 public void testSendNoAttachments() throws EmailException, IOException 061 { 062 this.getMailServer(); 063 064 String strSubject = "Test HTML Send #1 Subject (w charset)"; 065 066 this.email = new MockHtmlEmailConcrete(); 067 this.email.setHostName(this.strTestMailServer); 068 this.email.setSmtpPort(this.getMailServerPort()); 069 this.email.setFrom(this.strTestMailFrom); 070 this.email.addTo(this.strTestMailTo); 071 072 this.email.setAuthentication(this.strTestUser, this.strTestPasswd); 073 074 this.email.setCharset(Email.ISO_8859_1); 075 this.email.setSubject(strSubject); 076 077 URL url = new URL(EmailConfiguration.TEST_URL); 078 String cid = this.email.embed(url, "Apache Logo"); 079 080 String strHtmlMsg = 081 "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>"; 082 083 this.email.setHtmlMsg(strHtmlMsg); 084 this.email.setTextMsg( 085 "Your email client does not support HTML emails"); 086 087 this.email.send(); 088 this.fakeMailServer.stop(); 089 090 // validate txt message 091 validateSend( 092 this.fakeMailServer, 093 strSubject, 094 this.email.getTextMsg(), 095 this.email.getFromAddress(), 096 this.email.getToList(), 097 this.email.getCcList(), 098 this.email.getBccList(), 099 true); 100 101 // validate html message 102 validateSend( 103 this.fakeMailServer, 104 strSubject, 105 this.email.getHtmlMsg(), 106 this.email.getFromAddress(), 107 this.email.getToList(), 108 this.email.getCcList(), 109 this.email.getBccList(), 110 false); 111 } 112 113 /** 114 * @throws EmailException on an email error 115 * @throws IOException when sending fails, or a bad URL is used 116 */ 117 public void testSendWAttachments() throws EmailException, IOException 118 { 119 EmailAttachment attachment = new EmailAttachment(); 120 File testFile = null; 121 122 /** File to used to test file attachmetns (Must be valid) */ 123 testFile = File.createTempFile("commons-email-testfile", ".txt"); 124 125 // ==================================================================== 126 // Test Success 127 // ==================================================================== 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, this.strTestPasswd); 145 146 this.email.setCharset(Email.ISO_8859_1); 147 this.email.setSubject(strSubject); 148 149 URL url = new URL(EmailConfiguration.TEST_URL); 150 String cid = this.email.embed(url, "Apache Logo"); 151 152 String strHtmlMsg = 153 "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>"; 154 155 this.email.setHtmlMsg(strHtmlMsg); 156 this.email.setTextMsg( 157 "Your email client does not support HTML emails"); 158 159 this.email.send(); 160 this.fakeMailServer.stop(); 161 // validate txt message 162 validateSend( 163 this.fakeMailServer, 164 strSubject, 165 this.email.getTextMsg(), 166 this.email.getFromAddress(), 167 this.email.getToList(), 168 this.email.getCcList(), 169 this.email.getBccList(), 170 true); 171 172 // validate html message 173 validateSend( 174 this.fakeMailServer, 175 strSubject, 176 this.email.getHtmlMsg(), 177 this.email.getFromAddress(), 178 this.email.getToList(), 179 this.email.getCcList(), 180 this.email.getBccList(), 181 false); 182 183 // validate attachment 184 validateSend( 185 this.fakeMailServer, 186 strSubject, 187 attachment.getName(), 188 this.email.getFromAddress(), 189 this.email.getToList(), 190 this.email.getCcList(), 191 this.email.getBccList(), 192 false); 193 } 194 195 }