用idea使用maven开发javaMail并测试
github项目地址
项目总体结构:
1.png
** 1. 异常信息类:AccountEmailException.java**
package com.juvenxu.mvnbook.account.email;
public class AccountEmailException
extends Exception
{
private static final long serialVersionUID = -4817386460334501672L;
public AccountEmailException( String message )
{
super( message );
}
public AccountEmailException( String message, Throwable throwable )
{
super( message, throwable );
}
}
** 2.发送邮件类的接口 **
package com.juvenxu.mvnbook.account.email;
public interface AccountEmailService
{
void sendMail( String to, String subject, String htmlText )
throws AccountEmailException;
}
** 3. 发送邮件的接口类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 {
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);
} 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;
}
}
** spring的配置文件,配置AccountEmailService的信息,使用了spring的javaMailSender **
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">
<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.juvenxu.mvnbook.account.email.AccountEmailServiceImpl">
<property name="javaMailSender" ref="javaMailSender" />
<property name="systemEmail" value="${email.systemEmail}" />
</bean>
</beans>
service.properties配置文件,配置发送邮件的服务器:用的时候把email.username,email.password,email.systemEmail换成自己的:
email.protocol=smtp
email.host=smtp.163.com
email.port=25
email.username=chenwei182729@163.com
email.password=//这里填邮箱密码
email.auth=true
email.systemEmail=//这里填邮箱名称
** 测试**
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( "chenwei182729@163.com", "chen188290wei" );
greenMail.start();
}
@Test
public void testSendMail()
throws Exception
{
ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-email.xml" );
AccountEmailService accountEmailService = (AccountEmailService) ctx.getBean( "accountEmailService" );
String subject = "***同学";
String htmlText = "<h3>I Love You !!!</h3>";
accountEmailService.sendMail( "1510347223@qq.com", subject, htmlText );
greenMail.waitForIncomingEmail( 2000, 1 );
Message[] msgs = greenMail.getReceivedMessages();
assertEquals( 0, msgs.length );
assertEquals( subject, msgs[0].getSubject() );
assertEquals( htmlText, GreenMailUtil.getBody( msgs[0] ).trim() );
}
@After
public void stopMailServer()
throws Exception
{
greenMail.stop();
}
}
//运行 mvn test