项目POM文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.wh.learn.mvn
accountemail
0.0.1-SNAPSHOT
jar
accountemail
http://maven.apache.org
UTF-8
junit
junit
4.7
test
org.springframework
spring-core
2.5.6
org.springframework
spring-beans
2.5.6
org.springframework
spring-context
2.5.6
org.springframework
spring-context-support
2.5.6
javax.mail
1.4.1
com.icegreen
greenmail
1.3.1b
test
org.apache.maven.plugins
maven-compiler-plugin
1.5
1.5
POM文件解析:
在dependencies节点中,需要添加javax.mail的依赖;greenmail用来进行测试,其范围是test
Java主代码
package com.wh.learn.mvn.accountemail;
public interface AccountEmailService {
void sendMail(String to, String subject, String htmlText) throws AccountEmailException;
}
package com.wh.learn.mvn.accountemail;
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 {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
try {
//用于发送邮件的邮箱
msgHelper.setFrom(systemEmail);
//用于接收邮件的邮箱
msgHelper.setTo(to);
//邮件的主题
msgHelper.setSubject(subject);
//邮件的正文,第二个boolean类型的参数代表html格式
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;
}
}
package com.wh.learn.mvn.accountemail;
import javax.mail.MessagingException;
public class AccountEmailException extends MessagingException {
public AccountEmailException() {
super();
}
public AccountEmailException(String s, Exception e) {
super(s, e);
}
public AccountEmailException(String s) {
super(s);
}
}
xml配置&properties文件
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">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
class="org.springframework.mail.javamail.JavaMailSenderImpl">
${email.protocol}
class="com.wh.learn.mvn.accountemail.AccountEmailServiceImpl">
XML解析:
节点里面的定义一定要写对,如果出现报错,可能是因为xsd文档版本过高;
propertyConfigurer用来读取项目配置文件service.properties,即下面展示的properties文件;
根据从配置文件中读取到的配置,注入javaMailSender类中,构造一个对象;
将构造出来的JavaMailSender对象继续作为一个property注入到accountEmailService对象中。
email.protocol=smtps
email.host=smtp.163.com
email.port=465
email.username=your-username@163.com
email.password=your-password
email.auth=true
email.systemEmail=your-username@163.com
properties文件解析:
protocol设置为smtps,此处注意,如果用于发送邮件的邮箱没有开启smtp服务,则会在运行测试时报如下错误:org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException,以网易邮箱为例,开启smtp服务如下图所示;host也要设置为对应的smtp服务器;另外,使用smtp服务器进行登录邮箱,所用的port(端口号)为465,也要在配置文件中设置好;
username和password要输入真实有效的用户名和密码。
测试代码
package com.wh.learn.mvn.accountemail;
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;
public class TestAccountEmailService {
private GreenMail greenMail;
@Before
public void startMailServer() throws Exception{
greenMail = new GreenMail(ServerSetup.SMTP);
greenMail.setUser("your-username@163.cm","yourpasswordd");
greenMail.start();
}
@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 = "
Test
";
accountEmailService.sendMail("your-receive-mail@qq.com", subject, htmlText);
}
@After
public void stopMailServer() throws Exception{
greenMail.stop();
}
}
测试代码解析:
使用greenmail来测试我们的代码是否可用,具体代码如上所示;
通过getBean来获取AccountEmailService的实例;
仅仅测试了发送邮件,暂时只能靠登录邮箱来验证是否发送成功,后续会听过学习来补充验证的 方法。
心得体会
在编写POM,XML,properties文件时注意节点名称,节点属性等要写对,有时候花很多时间解决不了的问题往往是因为漏写了一个字母;
一个功能的实现不仅仅是代码的正确性确定的,有时候还有很多其他的因素,看待问题时眼光不能只局限于代码,更要放眼整个功能涉及到的所有模块。