1.spring配置文件的编写
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
">
<!-- 邮件服务 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.qq.com"/>
<property name="port" value="587"/><!--或许你可以用465端口,默认的25不适合-->
<property name="protocol" value="smtp"/>
<property name="username" value="tm_wmh@foxmail.com"/>
<property name="password" value="enqekrsrpudcigfe"/><!--这里的是你通过短信后,获取的授权码-->
<property name="defaultEncoding" value="UTF-8"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
</beans>
2.简单的发送测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applictionContext-mail.xml"})
public class MailTest {
@Autowired
JavaMailSender mailSender;
private static final String USERNAME = "tm_wmh@foxmail.com";
private static final String TO = "wu_meng_hao@foxmail.com";
/**
* 测试邮件发送
*/
@Test
public void sendMail() throws MessagingException {
//内容
String html = "<html><head>"+
"</head><body>"+
"<audio src='http://m10.music.126.net/20160422225433/25b43b999bcdaf3425b9194514340596/ymusic/8c94/b9af/69e3/7ebe35b8e00154120822550b21b0c9c5.mp3' autoplay='autoplay' controls='controls' loop='-1'>爱你</audio>"+
"<h1>Hello,Nice to meet you!</h1>"+
"<span style='color:red;font-size:36px;'>并摸了一把你的小奶</span>"+
"<img src='cid:javaxmail.png'>"+
"</body></html>";
//Message对象
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage,true);
//MessageHelper对象
messageHelper.setTo(TO);
messageHelper.setFrom(USERNAME);
messageHelper.setSubject("欢迎使用JavaMail!");
messageHelper.setText(html,true);
//发送
mailSender.send(mimeMessage);
}
}
3.携带附件发送邮件
/**
* 测试携带附件
*/
@Test
public void attachFile() throws MessagingException {
//内容
String html = "<html><head>"+
"</head><body>"+
"<audio src='http://m10.music.126.net/20160422225433/25b43b999bcdaf3425b9194514340596/ymusic/8c94/b9af/69e3/7ebe35b8e00154120822550b21b0c9c5.mp3' autoplay='autoplay' controls='controls' loop='-1'>爱你</audio>"+
"<h1>Hello,Nice to meet you!</h1>"+
"<span style='color:red;font-size:36px;'>并摸了一把你的小奶</span>"+
"<img src='cid:javaxmail.png'>"+
"</body></html>";
//Message对象
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage,true);
//关联附件文件
FileSystemResource file=new FileSystemResource(new File("C:\\Users\\Administrator\\OneDrive\\work\\jars\\slf4j-1.7.25.zip"));
//MessageHelper对象
messageHelper.setTo(TO);
messageHelper.setFrom(USERNAME);
messageHelper.setSubject("欢迎使用JavaMail!");
messageHelper.setText(html,true);
messageHelper.addAttachment(file.getFilename(),file);
//发送
mailSender.send(mimeMessage);
}
3.使用模板生成邮件内容
/**
* 使用模板生成邮件内容
*/
@Test
public void useTemplate() throws IOException, TemplateException, MessagingException {
Map map=new HashMap();
map.put("username","吴梦昊");
map.put("code",CodeUtils.getValidateCode());
//(1)生成邮件内容
String html = FreeMarkerUtils.getMailContent(map,"mail.ftl");
//(2)发送邮件
//Message对象
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage,true);
//MessageHelper对象
messageHelper.setTo(TO);
messageHelper.setFrom(USERNAME);
messageHelper.setSubject("欢迎使用JavaMail!");
messageHelper.setText(html,true);
//发送
mailSender.send(mimeMessage);
}
}
FreeMarker工具
/**
* FreeMarker工具类
*
* @author WuMenghao
*/
public class FreeMarkerUtils {
private FreeMarkerUtils() {
}
/**
* 生成邮件内容
*/
public static String getMailContent(Map map , String templateName) throws IOException, TemplateException {
//(1)从模板生成html
FreeMarkerConfigurer configurer=new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/template/mail");
configurer.setDefaultEncoding("utf-8");
//(2)获取Template对象
Configuration config=configurer.createConfiguration();
Template template = config.getTemplate(templateName);
//(3)获取Writer对象
StringWriter stringWriter=new StringWriter();
//(4)从生成html
template.process(map,stringWriter);
System.out.println(stringWriter.toString());
return stringWriter.toString();
}
}