项目结构
该部分为email模块,负责发送账号激活的电子邮件
account-email的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.juven.mvnbook.account</groupId>
<artifactId>account-email</artifactId>
<name>Account Email</name>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>2.5.6</version>
</dependency>
<!--实现邮件发送的依赖-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<!--GreenMail是开源的邮件服务测试套件,account-email模块使用该套件来测试邮件的发送。-->
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.3.1b</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--开启Java 5的支持-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
AccountEmailService.java
package com.juvenxu.mvnbook.account.email;
public interface AccountEmailService
{
//发送html格式的邮件,to为接收地址,subject为邮件主题,htmlText为邮件内容,如果发送邮件出错,
//则抛出AccountEmailException异常
void sendMail( String to, String subject, String htmlText )
throws AccountEmailException;
}
AccountEmailServiceImpl.java
package com.juvenxu.mvnbook.account.email;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class AccountEmailServiceImpl
implements AccountEmailService
{
//来自于Spring Framework的帮助简化邮件发送的工具类型,对应该字段有一组getter()和setter()
//方法,用来帮助实现依赖注入
private JavaMailSender javaMailSender;
private String systemEmail;
public void sendMail( String to, String subject, String htmlText )
throws AccountEmailException
{
try
{
//邮件服务器配置信息通过外部的配置注入到javaMailSender中
//创建一个MimeMessage,对应了将要发送的邮件
MimeMessage msg = javaMailSender.createMimeMessage();
//用于设置该邮件的发送地址、收件地址、主题以及内容
MimeMessageHelper msgHelper = new MimeMessageHelper( msg );
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;
}
}
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-2.5.xsd">
<!--帮助载入properties文件的组件-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath: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>
<!--注入javaMaiilSender-->
<bean id="accountEmailService"
class="com.juvenxu.mvnbook.account.email.AccountEmailServiceImpl">
<property name="javaMailSender" ref="javaMailSender" />
<property name="systemEmail" value="${email.systemEmail}" />
</bean>
</beans>
account-email的测试代码
AccountEmailServiceTest.java
package com.juvenxu.mvnbook.account.email;
import static junit.framework.Assert.assertEquals;
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;
public class AccountEmailServiceTest{
private GreenMail greenMail;
//在测试方法之前执行,启动邮件服务器
@Before
public void startMailServer()
throws Exception {
greenMail = new GreenMail( ServerSetup.SMTP );
greenMail.setUser( "test@juvenxu.com", "123456" );
greenMail.start();
}
@Test
public void testSendMail()
throws Exception {
//根据classpath路径中account-email.xml配置创建ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-email.xml" );
AccountEmailService accountEmailService =
(AccountEmailService) ctx.getBean( "accountEmailService" );
String subject = "Test Subject";
String htmlText = "<h3>Test</h3>";
accountEmailService.sendMail( "test2@juvenxu.com", subject, htmlText );
greenMail.waitForIncomingEmail( 2000, 1 );
Message[] msgs = greenMail.getReceivedMessages();
assertEquals( 1, msgs.length );
assertEquals( subject, msgs[0].getSubject() );
assertEquals( htmlText, GreenMailUtil.getBody( msgs[0] ).trim() );
}
//在测试方法时候执行,关闭邮件服务器
@After
public void stopMailServer()
throws Exception {
greenMail.stop();
}}