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.test.acount</groupId>
<artifactId>acount-email</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>acount-email</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<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>
</dependencies>
<build>
<!-- 不打包mail.properties -->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>mail.properties</exclude>
</excludes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
不打包依赖的jar,把依赖的jar copy到lib目录,和生成的jar放在同一级目录下
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.test.acount.email.AcountEmailServiceImpl</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
AcountEmailException 自定义异常
package com.test.acount.email;
public class AcountEmailException extends Exception{
private static final long serialVersionUID = 1L;
public AcountEmailException(){
super();
}
public AcountEmailException(String msg){
super(msg);
}
public AcountEmailException(String msg,Throwable e){
super(msg, e);
}
}
AcountEmailService 服务接口
package com.test.acount.email;
public interface AcountEmailService {
void sendEmail(String to,String subject,String htmlText) throws AcountEmailException;
}
AcountEmailServiceImpl 接口实现
package com.test.acount.email;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
/**
* 邮件发送接口
* @author mlw
*
*/
public class AcountEmailServiceImpl implements AcountEmailService {
private static Log log=LogFactory.getLog(AcountEmailServiceImpl.class);
private JavaMailSender javaMailSender;
private String systemEmail;
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;
}
public void sendEmail(String to, String subject, String htmlText) throws AcountEmailException {
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);
log.info("邮件发送成功");
} catch (Exception e) {
log.error("邮件发送失败");
throw new AcountEmailException("Faild to send mail",e);
}
}
public static void main(String[] args) {
ClassPathXmlApplicationContext cxa = new ClassPathXmlApplicationContext("acount-email.xml");
AcountEmailService aesi = (AcountEmailService) cxa.getBean("acountEmailService");
try {
aesi.sendEmail("*****@163.com", "hello word", "<h3>Test</h3>");
} catch (AcountEmailException e) {
e.printStackTrace();
}
}
}
acount-email.xml spring 配置
<?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:mail.properties"/> -->
<property name="location" value="file:src/main/resources/mail.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.host">${email.host}</prop>
<prop key="mail.transport.protocol">${email.protocol}</prop>
<prop key="mail.${email.protocol}.auth">${email.auth}</prop>
<prop key="mail.${email.protocol}.socketFactory.class">${email.socketFactory}</prop>
<prop key="mail.${email.protocol}.port">${email.port}</prop>
<prop key="mail.${email.protocol}.socketFactory.port">${email.port}</prop>
<prop key="mail.${email.protocol}.timeout">${email.timeout}</prop>
</props>
</property>
</bean>
<bean id="acountEmailService" class="com.test.acount.email.AcountEmailServiceImpl">
<property name="javaMailSender" ref="javaMailSender"/>
<property name="systemEmail" value="${email.systemEmail}"/>
</bean>
</beans>
mail.properties 发送方服务器配置
email.protocol=smtp
email.host=smtp.qq.com
email.port=465
email.username=******@qq.com
email.password=vkabqglhgrbngiaec
email.auth=true
email.systemEmail=******@qq.com
email.timeout=10000
email.socketFactory=javax.net.ssl.SSLSocketFactory
我使用的qq邮箱测试,一下配置基于qq邮箱
email.protocol=smtp
email.host=smtp.qq.com
email.port=465
email.username=******@qq.com 作为服务器的邮箱账号,自己的qq号
email.password=****** passwod不是qq号密码,进入自己邮箱-设置-账户-POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务-开启服务:
POP3/SMTP服务 (如何使用 Foxmail 等软件收发邮件?) 已开启 | 关闭 -----生成授权码,这里填的就是授权码
email.auth=true
email.systemEmail=******@qq.com 和username一样
email.timeout=10000
email.socketFactory=javax.net.ssl.SSLSocketFactory
AcountEmailServiceTest 测试类
package com.test.acount.email;
import javax.mail.internet.MimeMessage;
import static junit.framework.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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 AcountEmailServiceTest {
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 {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"test-acount-email.xml"); //src/test/resources下的properties,端口一定是25,否则GreenMail监听不到,配置中只保留3个配置项,否则编译不通过
AcountEmailService acountEmailService = (AcountEmailService) applicationContext
.getBean("acountEmailService");
String subject = "Test Subject";
String htmlText = "<h3>Test</h3>";
acountEmailService.sendEmail("666@qq.com", subject, htmlText);
greenMail.waitForIncomingEmail(2000, 1);
MimeMessage[] msgs = greenMail.getReceivedMessages();
assertEquals(1, msgs.length);
assertEquals(subject, msgs[0].getSubject());
assertEquals(htmlText, GreenMailUtil.getBody(msgs[0]).trim());
System.out.println(msgs[0].getFrom()[0]);
}
@After
public void stopMailServer()throws Exception{
greenMail.stop();
}
}
test-acount-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">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:test-mail.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.host">${email.host}</prop>
<prop key="mail.transport.protocol">${email.protocol}</prop>
<prop key="mail.${email.protocol}.auth">${email.auth}</prop>
<prop key="mail.${email.protocol}.socketFactory.class">${email.socketFactory}</prop>
<prop key="mail.${email.protocol}.port">${email.port}</prop>
<prop key="mail.${email.protocol}.socketFactory.port">${email.port}</prop>
<prop key="mail.${email.protocol}.timeout">${email.timeout}</prop>
</props>
</property>
</bean>
<bean id="acountEmailService" class="com.test.acount.email.AcountEmailServiceImpl">
<property name="javaMailSender" ref="javaMailSender"/>
<property name="systemEmail" value="${email.systemEmail}"/>
</bean>
</beans>
test-mail.properties
email.protocol=smtp
email.host=
email.port=25
email.username=
email.password=
email.auth=
email.systemEmail=xxx@qq.com
email.socketFactory=
email.timeout=