001: /*
002: * Dumbster - a dummy SMTP server
003: * Copyright 2004 Jason Paul Kitchen
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * 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 com.dumbster.smtp;
018:
019: import junit.framework.TestCase;
020:
021: import javax.mail.Session;
022: import javax.mail.Message;
023: import javax.mail.Transport;
024: import javax.mail.MessagingException;
025: import javax.mail.internet.MimeMessage;
026: import javax.mail.internet.InternetAddress;
027: import java.util.Properties;
028: import java.util.Date;
029: import java.util.Iterator;
030:
031: public class SimpleSmtpServerTest extends TestCase {
032: private static final int SMTP_PORT = 1081;
033:
034: SimpleSmtpServer server;
035:
036: public SimpleSmtpServerTest(String s) {
037: super (s);
038: }
039:
040: protected void setUp() throws Exception {
041: super .setUp();
042: server = SimpleSmtpServer.start(SMTP_PORT);
043: }
044:
045: protected void tearDown() throws Exception {
046: super .tearDown();
047: server.stop();
048: }
049:
050: public void testSend() {
051: try {
052: sendMessage(SMTP_PORT, "sender@here.com", "Test",
053: "Test Body", "receiver@there.com");
054: } catch (Exception e) {
055: e.printStackTrace();
056: fail("Unexpected exception: " + e);
057: }
058:
059: assertTrue(server.getReceivedEmailSize() == 1);
060: Iterator emailIter = server.getReceivedEmail();
061: SmtpMessage email = (SmtpMessage) emailIter.next();
062: assertTrue(email.getHeaderValue("Subject").equals("Test"));
063: assertTrue(email.getBody().equals("Test Body"));
064: }
065:
066: public void testSendMessageWithCarriageReturn() {
067: String bodyWithCR = "\n\nKeep these pesky carriage returns\n\n";
068: try {
069: sendMessage(SMTP_PORT, "sender@hereagain.com", "CRTest",
070: bodyWithCR, "receivingagain@there.com");
071: } catch (Exception e) {
072: e.printStackTrace();
073: fail("Unexpected exception: " + e);
074: }
075:
076: assertTrue(server.getReceivedEmailSize() == 1);
077: Iterator emailIter = server.getReceivedEmail();
078: SmtpMessage email = (SmtpMessage) emailIter.next();
079: assertTrue(email.getBody().equals(bodyWithCR));
080: }
081:
082: public void testSendTwoMessagesSameConnection() {
083: try {
084: MimeMessage[] mimeMessages = new MimeMessage[2];
085: Properties mailProps = getMailProperties(SMTP_PORT);
086: Session session = Session.getInstance(mailProps, null);
087: //session.setDebug(true);
088:
089: mimeMessages[0] = createMessage(session,
090: "sender@whatever.com", "receiver@home.com",
091: "Doodle1", "Bug1");
092: mimeMessages[1] = createMessage(session,
093: "sender@whatever.com", "receiver@home.com",
094: "Doodle2", "Bug2");
095:
096: Transport transport = session.getTransport("smtp");
097: transport.connect("localhost", SMTP_PORT, null, null);
098:
099: for (int i = 0; i < mimeMessages.length; i++) {
100: MimeMessage mimeMessage = mimeMessages[i];
101: transport.sendMessage(mimeMessage, mimeMessage
102: .getAllRecipients());
103: }
104:
105: transport.close();
106: } catch (MessagingException e) {
107: e.printStackTrace();
108: fail("Unexpected exception: " + e);
109: }
110:
111: assertTrue(server.getReceivedEmailSize() == 2);
112: }
113:
114: public void testSendTwoMsgsWithLogin() {
115: try {
116: String Server = "localhost";
117: String From = "sender@here.com";
118: String To = "receiver@there.com";
119: String Subject = "Test";
120: String body = "Test Body";
121:
122: Properties props = System.getProperties();
123:
124: if (Server != null) {
125: props.put("mail.smtp.host", Server);
126: }
127:
128: Session session = Session.getDefaultInstance(props, null);
129: Message msg = new MimeMessage(session);
130:
131: if (From != null) {
132: msg.setFrom(new InternetAddress(From));
133: } else {
134: msg.setFrom();
135: }
136:
137: InternetAddress.parse(To, false);
138: msg.setRecipients(Message.RecipientType.TO, InternetAddress
139: .parse(To, false));
140: msg.setSubject(Subject);
141:
142: msg.setText(body);
143: msg.setHeader("X-Mailer", "musala");
144: msg.setSentDate(new Date());
145: msg.saveChanges();
146:
147: Transport transport = null;
148:
149: try {
150: transport = session.getTransport("smtp");
151: transport.connect(Server, SMTP_PORT, "ddd", "ddd");
152: transport.sendMessage(msg, InternetAddress.parse(To,
153: false));
154: transport.sendMessage(msg, InternetAddress.parse(
155: "dimiter.bakardjiev@musala.com", false));
156:
157: } catch (javax.mail.MessagingException me) {
158: me.printStackTrace();
159: } catch (Exception e) {
160: e.printStackTrace();
161: } finally {
162: if (transport != null) {
163: transport.close();
164: }
165: }
166: } catch (Exception e) {
167: e.printStackTrace();
168: }
169:
170: assertTrue(server.getReceivedEmailSize() == 2);
171: Iterator emailIter = server.getReceivedEmail();
172: SmtpMessage email = (SmtpMessage) emailIter.next();
173: assertTrue(email.getHeaderValue("Subject").equals("Test"));
174: assertTrue(email.getBody().equals("Test Body"));
175: }
176:
177: private Properties getMailProperties(int port) {
178: Properties mailProps = new Properties();
179: mailProps.setProperty("mail.smtp.host", "localhost");
180: mailProps.setProperty("mail.smtp.port", "" + port);
181: mailProps.setProperty("mail.smtp.sendpartial", "true");
182: return mailProps;
183: }
184:
185: private void sendMessage(int port, String from, String subject,
186: String body, String to) throws MessagingException {
187: Properties mailProps = getMailProperties(port);
188: Session session = Session.getInstance(mailProps, null);
189: //session.setDebug(true);
190:
191: MimeMessage msg = createMessage(session, from, to, subject,
192: body);
193: Transport.send(msg);
194: }
195:
196: private MimeMessage createMessage(Session session, String from,
197: String to, String subject, String body)
198: throws MessagingException {
199: MimeMessage msg = new MimeMessage(session);
200: msg.setFrom(new InternetAddress(from));
201: msg.setSubject(subject);
202: msg.setSentDate(new Date());
203: msg.setText(body);
204: msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
205: to));
206: return msg;
207: }
208: }
|