使用greenmail测试发送邮件

1、自定义异常类AccountEmailException
public   class  AccountEmailException
    
extends  Exception
{
    
private   static   final   long  serialVersionUID  =   - 4817386460334501672L ;

    
public  AccountEmailException( String message )
    {
        
super ( message );
    }

    
public  AccountEmailException( String message, Throwable throwable )
    {
        
super ( message, throwable );
    }
}

2、发邮件的接口类
public   interface  AccountEmailService
{
    
/**
     *         发送邮件
     *          to为接收地址,subject为邮件主题,htmlText为邮件内容
      *   
@author  lanjh 上午10:51:45
      *   
@return  void
      *   
@throws   抛出异常说明
     
*/
    
void  sendMail( String to, String subject, String  ht mlText )
        
throws  AccountEmailException;
}



3、接口实现类
public   class  AccountEmailServiceImpl
    
implements  AccountEmailService
{
    
// spring framework中帮助邮件发送工具类
     private  JavaMailSender javaMailSender;

    
private  String systemEmail;

    
public   void  sendMail( String to, String subject, String htmlText )
        
throws  AccountEmailException
    {
        
try
        {
            
// msg对应着将要发送的邮件
            MimeMessage msg  =  javaMailSender.createMimeMessage();
            
// 帮助设置邮件msg相关信息
            MimeMessageHelper msgHelper  =   new  MimeMessageHelper( msg );
            
// systemEmail 系统邮箱设置
            msgHelper.setFrom( systemEmail );
            msgHelper.setTo( to );
            msgHelper.setSubject( subject );
            
// true表示邮件内容为html格式
            msgHelper.setText( htmlText,  true  );

            javaMailSender.send( msg );
        }
        
catch  ( MessagingException e )
        {
            
throw   new  AccountEmailException(  " Faild to send mail. " , e );
        }
    }

    
public  JavaMailSender getJavaMailSender()
    {
        
return  javaMailSender;
    }

    
public   void  setJavaMailSender( JavaMailSender javaMailSender )
    {
        
this .javaMailSender  =  javaMailSender;
    }

    
public  String getSystemEmail()
    {
        
return  systemEmail;
    }

    
public   void  setSystemEmail( String systemEmail )
    {
        
this .systemEmail  =  systemEmail;
    }
}


4、测试类

public   class  AccountEmailServiceTest
{
    
private  GreenMail greenMail;

    @Before
    
public   void  startMailServer()
        
throws  Exception
    {
        greenMail 
=   new  GreenMail( ServerSetup.SMTP );
        greenMail.setUser( 
" lanjh " " ****** "  );
        greenMail.start();
    }

    @Test
    
public   void  testSendMail()
        
throws  Exception
    {
        ApplicationContext ctx 
=   new  ClassPathXmlApplicationContext(  " account-email.xml "  );
        AccountEmailService accountEmailService 
=  (AccountEmailService) ctx.getBean(  " accountEmailService "  );

        String subject 
=   " Test Subject " ;
        String htmlText 
=   " <h3>Test</h3> " ;
        accountEmailService.sendMail( 
" 275581963@qq.com " , subject, htmlText );

        greenMail.waitForIncomingEmail( 
2000 1  );

        Message[] msgs 
=  greenMail.getReceivedMessages();
        assertEquals( 
1 , msgs.length );
        assertEquals( 
" lanjh@chinalmtc.com " , msgs[ 0 ].getFrom()[ 0 ].toString() );
        assertEquals( subject, msgs[
0 ].getSubject() );
        assertEquals( htmlText, GreenMailUtil.getBody( msgs[
0 ] ).trim() );
    }

    @After
    
public   void  stopMailServer()
        
throws  Exception
    {
        greenMail.stop();
    }
}

5、相关配置文件

account-mail.xml

<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>

    
< bean  id ="propertyConfigurer"
        class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        
< property  name ="location"  value ="classpath:account-service.properties"   />
    
</ bean >

    
< bean  id ="javaMailSender"  class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
        
< property  name ="protocol"  value ="${email.protocol}"   />
        
< property  name ="host"  value ="${email.host}"   />
        
< property  name ="port"  value ="${email.port}"   />
        
< property  name ="username"  value ="${email.username}"   />
        
< property  name ="password"  value ="${email.password}"   />
        
< property  name ="javaMailProperties" >
            
< props >
                
< prop  key ="mail.${email.protocol}.auth" > ${email.auth} </ prop >
            
</ props >
        
</ property >
    
</ bean >

    
< bean  id ="accountEmailService"
        class
="com.juvenxu.mvnbook.account.email.AccountEmailServiceImpl" >
        
< property  name ="javaMailSender"  ref ="javaMailSender"   />
        
< property  name ="systemEmail"  value ="${email.systemEmail}"   />
    
</ bean >
</ beans >

account-service.properties

email.protocol=smtp
email.host=localhost
email.port=25
email.username=lanjh
email.password=*****
email.auth=true
email.systemEmail=lanjh@chinalmtc.com


greenmail  api参考
http://www.icegreen.com/greenmail/javadocs/overview-summary.html
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值