1.在Eclipse中新建Maven工程。
2.修改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.iteye.xujava</groupId> <artifactId>account-email</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>account-email</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <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>com.icegreen</groupId> <artifactId>greenmail</artifactId> <version>1.3.1b</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies> </project>
2.在src/main/java的包com.iteye.xujava.account.email下新建三个文件AccountEmailException、AccountEmailService、AccountEmailServiceImpl
package com.iteye.xujava.account.email; public class AccountEmailException extends Exception { private static final long serialVersionUID = 6514881539290222459L; public AccountEmailException(String message) { super(message); } public AccountEmailException(String message, Throwable throwable) { super(message, throwable); } }
package com.iteye.xujava.account.email; public interface AccountEmailService { void sendMail(String to,String subject,String htmlText) throws AccountEmailException; }
package com.iteye.xujava.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 { private JavaMailSender javaMailSender; private String systemEmail; public void sendMail(String to, String subject, String htmlText) throws AccountEmailException { 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); System.out.println("发送完毕"); } catch (MessagingException e) { throw new AccountEmailException("发送邮件失败...", 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; } }
3.在src/main/resources目录下新建account-email.xml和service.properties文件
<?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: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.iteye.xujava.account.email.AccountEmailServiceImpl"> <property name="javaMailSender" ref="javaMailSender" /> <property name="systemEmail" value="${email.systemEmail}" /> </bean> </beans>
#真实 #email.protocol=smtp #email.host=smtp.163.com #email.port=25 #[email protected] #email.password=password #email.auth=true #[email protected] #测试 email.protocol=smtp email.host=127.0.0.1 email.port=25 [email protected] email.password=123456 email.auth=true [email protected]
4.在src/test/java的包 com.iteye.xujava.account.email下新建测试文件
package com.iteye.xujava.account.email; import static org.junit.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("[email protected]", "123456"); greenMail.start(); } @Test public void testSendMail() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("account-email.xml"); AccountEmailService accountEmailService = (AccountEmailService) ctx.getBean("accountEmailService"); String subject = "Text Subject"; String htmlText = "<h3>Test</h3>"; accountEmailService.sendMail("[email protected]", 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 stopMainServer() throws Exception { greenMail.stop(); } }
5.在命令提示行找到account-email目录,输入mvn clean test测试,成功后,输入mvn clean install安装到Maven库。