public class AccountEmailServiceTest {
@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>I Love You</h3>";
accountEmailService.sendMail("zhaoyan8783@qq.com", subject, htmlText);
}
}
前几天搞spring+hibernate+struts2,其中一些库的依赖关系搞的我头痛,所以为了避免重蹈覆辙,开始研究一下maven,对以后的项目开发管理都有帮助。
安装maven和安装eclipse插件的文章网上有很多,我就不多说了,默认我的电脑已经有了jdk5,安装了maven3,并安装了eclipse插件。
1.eclipse里建个maven项目:
项目目录基本都是固定的,如下:
在项目根目录下修改pom.xml,内容如下:
<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.ease.account</groupId> <artifactId>account-email</artifactId> <version>0.0.1-SNAPSHOT</version> <name>email</name> <description>发送邮件</description> <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> </dependencies> </project>
其中
<modelVersion>4.0.0</modelVersion> <groupId>com.ease.account</groupId> <artifactId>account-email</artifactId> <version>0.0.1-SNAPSHOT</version> <name>email</name> <description>发送邮件</description>
是这个新建项目的坐标,
<dependencies> ........ </dependencies>
是项目依赖的库,当我们保存pom.xml文件的时候,maven会自动为我们下载XML文件中添加的依赖库,如图:
其中也包括一些上面没有显式声明依赖的,比如commons-logging-1.1.1.jar,那是因为spring-core-2.5.6.jar依赖commons-logging-1.1.1.jar,所以maven也把它下载了,spring-core依赖关系可以参考maven仓库中spring-core目录下的pom文件。
接下来编写具体类:
首先定义邮件发送接口,很简单,就一个方法
public interface AccountEmailService {
void sendMail(String to, String subject, String htmlText) throws Exception;
}
下面是实现类
public class AccountEmailServiceImpl implements AccountEmailService {
private JavaMailSender javaMailSender;
private String systemEmail;
public void sendMail(String to, String subject, String htmlText)
throws Exception {
try {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
msgHelper.setFrom(systemEmail);
msgHelper.setTo(to);
msgHelper.setSubject(subject);
msgHelper.setText(htmlText, true);
javaMailSender.send(msg);
} catch(MessagingException e) {
throw new Exception("发送邮件失败!", e);
}
}
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void setSystemEmail(String systemEmail) {
this.systemEmail = systemEmail;
}
}
实现类中的实例属性都是通过spring注入进来,spring配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsl="http://www.w3.org/2001/XMLSchema-instance" xsl: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:service.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.ease.account.email.AccountEmailServiceImpl"> <property name="javaMailSender" ref="javaMailSender"></property> <property name="systemEmail" value="${email.systemEmail}"></property> </bean> </beans>
配置文件中也用到了属性文件,这样更加灵活
email.protocol=smtps email.host=smtp.gmail.com email.port=465 email.username=***@gmail.com email.password=*** email.auth=true email.systemEmail=***@qq.com
最后,再写一个测试类
public class AccountEmailServiceTest {
@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>I Love You</h3>";
accountEmailService.sendMail("zhaoyan8783@qq.com", subject, htmlText);
}
}
这个测试类中并没有断言,简单了点,呵呵
好的,最后就是maven大展身手的时候了,在项目名上或者pom.xml上点鼠标右键,如图:
执行Maven test,
如果CONSOLE中显示BUILD SUCCESS,则说明测试类成功执行了,并且,图片右下角显示我已经收到了这个邮件了,呵呵。
接下来我们要做的是打包项目,执行Run as下面的Maven install,执行OK以后,我们会在项目的target下面找到打包的文件,如图:
并且Maven已经把这个项目包放在了Maven仓库中,其它项目可以依赖使用。
源代码下载地址
http://download.csdn.net/detail/zhaoyan8783/4008290