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

字体大小: 中小 标准 ->行高大小: 标准
比较流行的Java的SMTP开源组件有: 


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

引用
GreenMail is an embeddable, lightweight and sandboxed email server for testing and developing purposes


http://www.icegreen.com/greenmail/ 

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

Java代码  
  1. private static void testSMTPCode() throws Exception {  
  2.     GreenMail greenMail = new GreenMail();  
  3.     greenMail.start();  
  4.       
  5.     // Sending  
  6.     GreenMailUtil.sendTextEmailTest("to@localhost.com""from@localhost.com""subject""body");  
  7.     // Retrieving  
  8.     String msg = GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]);  
  9.     System.out.println(msg);  
  10.       
  11.     greenMail.stop();  
  12. }  


Java代码  
  1. private static void testSMTPSCode() throws Exception {  
  2.     GreenMail greenMail = new GreenMail();  
  3.     greenMail.start();  
  4.       
  5.     // Sending  
  6.     GreenMailUtil.sendTextEmailSecureTest("to@localhost.com""from@localhost.com""subject""body");  
  7.     // Retrieving  
  8.     String msg = GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]);  
  9.     System.out.println(msg);  
  10.       
  11.     greenMail.stop();  
  12. }  


Java代码  
  1. private static void testSpecialPort() throws Exception {  
  2.     ServerSetup ss = new ServerSetup(3025null"smtp");  
  3.       
  4.     GreenMail greenMail = new GreenMail(ss);  
  5.     greenMail.start();  
  6.     GreenMailUtil.sendTextEmail("to@localhost.com""from@localhost.com""subject""body", ss);  
  7.   
  8.     greenMail.stop();  
  9. }  

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/68310.html