使用greenmail测试发送邮件1、自定义异常类AccountEmailException public?class?AccountEmailException????
使用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?htmlText?)
????????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 楼 s58136698 2012-04-01 我按照上述方法写了测试方法,只是修改了邮箱地址
运行后能够给我的邮箱发送邮件,但是greenMail.getReceivedMessages();
得到的messages[] 为0.。不知道是为什么?
greenMail 只能接收自己个的邮件吗?