当我们系统要用Junit 测试批量发邮件功能的时候,
既要测试 这功能是否正常。
但又不能真正意义上的把邮件发出去。
要不你在测试的时候 那么多垃圾邮件给用户 用户肯定要烦恼了。
问题解决:
在这里
http://quintanasoft.com/dumbster/ 有个专门 测试 发Email 开源类库。
详细说明点击该网站查看。
一个示例
package com.dumbster.mytest;
import junit.framework.TestCase;
import com.dumbster.smtp.*;
import junit.framework.TestCase;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import java.util.Properties;
import java.util.Date;
import java.util.Iterator;
import java.util.*;
/** *//**
* @author junmy
*
*/
public class SmtpServerTest extends TestCase {
private SimpleSmtpServer server;
private static final int SMTP_PORT=1082;
public SmtpServerTest(String s)
{
super(s);
}
protected void setUp() throws Exception
{
super.setUp();
server=SimpleSmtpServer.start(SMTP_PORT);
}
protected void tearDown() throws Exception
{
super.tearDown();
server.stop();
}
public void testSendBacthMail()
{
String From = "sender@here.com";
String Subject = "Test";
String body = "Test Body";
//TODO:
// dal.BacthMail()
//得到后台数据库中所有用户得email
List<String> list=new ArrayList<String>();
//list=dal.BacthMail();
Properties mailProps = new Properties();
mailProps.setProperty("mail.smtp.host", "localhost");
mailProps.setProperty("mail.smtp.port", "" + SMTP_PORT);
mailProps.setProperty("mail.smtp.sendpartial", "true");
Session session = Session.getInstance(mailProps, null);
for(String strto:list)
{
try
{
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(From));
msg.setSubject(Subject);
msg.setSentDate(new Date());
msg.setText(body);
System.out.println("emailList:"+strto);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(strto));
Transport.send(msg);
}
catch (MessagingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
fail("Exception:"+e);
}
}
assertEquals(list.size(), server.getReceivedEmailSize());
Iterator emailIter = server.getReceivedEmail();
for(int i=0;i<server.getReceivedEmailSize();i++)
{
SmtpMessage email = (SmtpMessage) emailIter.next();
assertEquals(body,email.getBody());
assertEquals(Subject,email.getHeaderValue("Subject"));
assertEquals(list.get(i),email.getHeaderValue("To"));
}
}
}
详细说明请下载源代码查阅
http://quintanasoft.com/dumbster/
http://sourceforge.net/projects/dumbster
Dot Net 版本
http://ndumbster.sourceforge.net/
dot net 下示例
Nunit Test Batch Email
SimpleSmtpServer server;
public BatchEmailTest()
{
server = null;
server = SimpleSmtpServer.Start();
}
[Test]
public void TestBatchEmail()
{
ISiteUserService iSiteUser = ServiceFactory.GetInstance().GetService(typeof(ISiteUserService)) as ISiteUserService;
Hashtable hs = new Hashtable();
IList mailList = iSiteUser.GetBatchEmailList(hs);
SmtpMail.SmtpServer = "localhost";
for (int i = 0; i < mailList.Count; i++)
{
//send mail
MailMessage message = new MailMessage();
message.Subject = "Subject";
message.Body = "Send Body..";
message.From = "test@cwave.com";
message.To = mailList[i].ToString();
SmtpMail.Send(message);
}
Assert.AreEqual(mailList.Count, server.ReceivedEmailCount);
for (int j = 0; j < server.ReceivedEmailCount; j++)
{
SmtpMessage email = server.ReceivedEmail[j];
Assert.AreEqual("<test@cwave.com>", email.Headers["From"]);
Assert.AreEqual("<" + mailList[j].ToString() + ">", email.Headers["To"]);
Assert.AreEqual("Subject", email.Headers["Subject"]);
Assert.AreEqual("Send Body..", email.Body);
}
}
[TestFixtureTearDown]
public void TearDown()
{
if (server != null)
server.Stop();
}
}
希望对你有用!