Java单元测试之邮件测试-GreenMail

比较流行的Java的SMTP开源组件有:
[list]
[*]Apache James Server [url=http://james.apache.org/]http://james.apache.org/[/url]
[*]JES(Java Email Server) [url=http://www.ericdaugherty.com/java/mailserver/]http://www.ericdaugherty.com/java/mailserver/[/url]
[*]SubEthaSMTP [url=http://code.google.com/p/subethasmtp/]http://code.google.com/p/subethasmtp/[/url]
[*]Dumbster [url=http://quintanasoft.com/dumbster/]http://quintanasoft.com/dumbster/[/url]
[*]GreenMail [url=http://www.icegreen.com/greenmail/]http://www.icegreen.com/greenmail/[/url]
[/list]

其中 GreenMail 是最适合做单元测试的,它本身并不会发送邮件,但支持SMTP, POP3, IMAP。发布的时间也很长了,不过知道的人并不多。如果做发送邮件代码的单元测试,不想走邮件服务器,也不想mock的话,GreenMail 是个很好的选择。

[quote]GreenMail is an embeddable, lightweight and sandboxed email server for testing and developing purposes[/quote]

[url=http://www.icegreen.com/greenmail/]http://www.icegreen.com/greenmail/[/url]

目前版本:greenmail-1.3.1b.jar
Roadmap显示在1.4+版本中将会用 SubEthaSMTP 代替目前GreenMail自身的SMTP代码。


private static void testSMTPCode() throws Exception {
GreenMail greenMail = new GreenMail();
greenMail.start();

// Sending
GreenMailUtil.sendTextEmailTest("to@localhost.com", "from@localhost.com", "subject", "body");
// Retrieving
String msg = GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]);
System.out.println(msg);

greenMail.stop();
}



private static void testSMTPSCode() throws Exception {
GreenMail greenMail = new GreenMail();
greenMail.start();

// Sending
GreenMailUtil.sendTextEmailSecureTest("to@localhost.com", "from@localhost.com", "subject", "body");
// Retrieving
String msg = GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]);
System.out.println(msg);

greenMail.stop();
}



private static void testSpecialPort() throws Exception {
ServerSetup ss = new ServerSetup(3025, null, "smtp");

GreenMail greenMail = new GreenMail(ss);
greenMail.start();
GreenMailUtil.sendTextEmail("to@localhost.com", "from@localhost.com", "subject", "body", ss);

greenMail.stop();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值