1、GreenMail简介
GreenMail是一个用于测试的、开源的、直观的和易于使用的邮件服务器测试套件。它支持 SMTP、POP3、IMAP 等。它还为JBoss提供GreenMail服务。
GreenMail是第一个并且是唯一的一个为从Java
接收
和检索
电子邮件
提供
测试框架的库
。主页:http://www.icegreen.com/greenmail/。
2、GreenMail在实际项目中的使用
(1)项目的POM文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jackie.codeproject.account</groupId>
<artifactId>account-email</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>account-email</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.3.1b</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
(2) 邮件发送接口:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.account.email;
/**
* Title: AccountEmailService.java
* 邮件接口
*
* @author jackie
* @since Mar 30, 2013 5:58:07 PM
* @version V1.0
*/
public interface AccountEmailService {
/**
* 发送邮件方法
* @author jackie
* @date Mar 30, 2013
* @param to
* @param subject
* @param htmlText
* @throws Exception
* @return void
*/
void sendEmail(String to, String subject, String htmlText) throws AccountEmailException;
}
(2) 邮件发送接口实现类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.account.email;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
/**
* Title: AccountEmailServiceImpl.java
* 邮件发送的实现类
*
* @author jackie
* @since Mar 30, 2013 6:11:40 PM
* @version V1.0
*/
public class AccountEmailServiceImpl implements AccountEmailService {
/**
* @Fields javaMailSender : 简化邮件发送的工具类
*/
private JavaMailSender javaMailSender;
/**
* @Fields systemEmail : 系统邮箱账号
*/
private String systemEmail;
public void sendEmail(String to, String subject, String htmlText) throws AccountEmailException{
try {
// 使用javaMailSender创建一个MimeMessage msg,对应了将要发送的邮件
MimeMessage msg = javaMailSender.createMimeMessage();
// 通过MimeMessage创建一个MimeMessageHelper msgHelper
MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
// 使用MimeMessageHelper设置邮件的发送地址、收件地址、主题以及内容
msgHelper.setFrom(systemEmail);
msgHelper.setTo(to);
msgHelper.setSubject(subject);
msgHelper.setText(htmlText, true);
// 发送邮件
javaMailSender.send(msg);
} catch (MessagingException e) {
throw new AccountEmailException("Failed to send email.", e);
}
}
/**
* @return the javaMailSender
*/
public JavaMailSender getJavaMailSender() {
return javaMailSender;
}
/**
* @param javaMailSender
* the javaMailSender to set
*/
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
/**
* @return the systemEmail
*/
public String getSystemEmail() {
return systemEmail;
}
/**
* @param systemEmail
* the systemEmail to set
*/
public void setSystemEmail(String systemEmail) {
this.systemEmail = systemEmail;
}
}
(4)自定义异常类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.account.email;
/**
* Title: AccountEmailException.java 自定义邮件异常
*
* @author jackie
* @since Mar 30, 2013 6:01:31 PM
* @version V1.0
*/
public class AccountEmailException extends Exception {
/**
* @Fields serialVersionUID
*/
private static final long serialVersionUID = -5399025975888079256L;
/**
* 错误消息
*/
private String message;
public AccountEmailException(String message) {
this.message = message;
}
public AccountEmailException(String message, Throwable throwable) {
super(message, throwable);
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
}
(5)Spring配置文件account-email.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-3.2.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:email.properties"></property>
</bean>
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="protocol" value="${email.protocol}"></property>
<property name="host" value="${email.host}"></property>
<property name="port" value="${email.port}"></property>
<property name="username" value="${email.username}"></property>
<property name="password" value="${email.password}"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.${email.protocol}.auth">${email.auth}</prop>
</props>
</property>
</bean>
<bean id="accountEmailService" class="com.jackie.codeproject.account.email.AccountEmailServiceImpl">
<property name="javaMailSender" ref="javaMailSender">
</property>
<property name="systemEmail" value="${email.systemEmail}"></property>
</bean>
</beans>
(6)属性文件email.properties:
#协议
email.protocol=smtp
#服务器
email.host=localhost
#端口
email.port=25
#用户名
#密码
email.password=123456
#服务器需要用户名和密码进行认证
email.auth=true
#系统邮箱
(7)测试类:
/*
* Copyright 2013-2015
*/
package com.jackie.codeproject.account.email;
import static org.junit.Assert.*;
import javax.mail.Message;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetup;
/**
* Title: AccountEmailServiceTest.java 邮件服务测试类
*
* @author jackie
* @since Mar 30, 2013 6:13:14 PM
* @version V1.0
*/
public class AccountEmailServiceTest {
private GreenMail greenMail;
private ApplicationContext applicationContext;
/**
* 启动邮件服务器
*
* @author jackie
* @date Mar 30, 2013
* @throws Exception
* @return void
*/
@Before
public void setUp() throws Exception {
greenMail = new GreenMail(ServerSetup.SMTP);
greenMail.start();
}
/**
* Test method for
* {@link com.jackie.codeproject.account.email.AccountEmailService#sendEmail(java.lang.String, java.lang.String, java.lang.String)}
*
*/
@Test
public void testSendEmail() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("account-email.xml");
AccountEmailService accountEmailService = (AccountEmailService) applicationContext.getBean("accountEmailService");
String subject = "Test Subject";
String htmlText = "<h3> Test </h3>";
greenMail.waitForIncomingEmail(2000, 1);
Message[] msgs = greenMail.getReceivedMessages();
assertEquals(1, msgs.length);
assertEquals(subject, msgs[0].getSubject());
assertEquals(htmlText, GreenMailUtil.getBody(msgs[0]).trim());
}
/**
* 关闭邮件服务器
*
* @author jackie
* @date Mar 30, 2013
* @throws Exception
* @return void
*/
@After
public void tearDown() throws Exception {
greenMail.stop();
}
}