一、Maven项目依赖
<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.manxu</groupId>
<artifactId>hello_world_archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello_world_archetype</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>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.3.RELEASE</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>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、发送邮件接口:
package com.manxu.archetype.account.email;
import javax.security.auth.login.AccountException;
public interface AccountEmailService {
void senMail(String to,String subject, String context) throws AccountException;
}
三、发送邮件接口实现类
package com.manxu.archetype.account.email;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.security.auth.login.AccountException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class AccountEmailServiceImp implements AccountEmailService{
private JavaMailSender javaMailSender;
private String systemEmail;
@Override
public void senMail(String to, String subject, String context) throws AccountException{
try {
MimeMessage my = javaMailSender.createMimeMessage();
MimeMessageHelper help = new MimeMessageHelper(my);
help.setFrom(systemEmail);
help.setTo(to);
help.setSubject(subject);
help.setText(context);
javaMailSender.send(my);
} catch (Exception e) {
e.printStackTrace();
}
}
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;
}
}
四、配置文件
#email.protocol=smtps
#email.host=smtp.163.com
#email.port=465
#email.username=zhangchengjava1@163.com
#email.password=zhaojuan1
#email.auth=true
#email.systemEmail=zhangchengjava1@163.com
## greenmail 测试不需要服务器,可以把自身当前服务器,并提供虚拟用户,如本例发件 test@gmail.com,收件test2@gmail.com
email.protocol=smtp
email.host=localhost
email.port=25
email.username=test@gmail.com
email.password=123456
email.auth=true
email.systemEmail=zhangchengjava1@163.com
五、测试类
package com.manxu.archetype.account.email;
import javax.mail.Message;
import javax.security.auth.login.AccountException;
import junit.framework.Assert;
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.ServerSetup;
import com.icegreen.greenmail.util.ServerSetupTest;
public class AccountEmailServiceTest {
private GreenMail greenMail;
@Before
public void startMailServer(){
greenMail = new GreenMail(ServerSetup.SMTP);
greenMail.setUser("test2@gmail.com", "123456");
greenMail.start();
}
@Test
public void testSendMail() throws Exception{
ApplicationContext ctx = new ClassPathXmlApplicationContext("account-email.xml");
AccountEmailService service = (AccountEmailService) ctx.getBean("accountEmailService");
String subject = "text subject";
String htmlText = "<h3>text</h3>";
service.senMail("test2@gmail.com", subject, htmlText);
greenMail.waitForIncomingEmail(2000, 1);
Message[] msgs = greenMail.getReceivedMessages();
Assert.assertEquals(1, msgs.length);
Assert.assertEquals(subject, msgs[0].getSubject());
}
@After
public void stopMailServer(){
greenMail.stop();
}
}