<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.17.RELEASE</version> </dependency>
package com.xmyself.mail;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class Main {
public static void main(String[] args) {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.qq.com");
mailSender.setPort(587);
mailSender.setUsername("573215750@qq.com");
mailSender.setPassword("dsruklozelxcbdba");//授权码
SimpleMailMessage mail = new SimpleMailMessage();
mail.setTo("573215750@qq.com");
mail.setFrom("573215750@qq.com");
mail.setSubject("test mail");
mail.setText("test mail content");
mailSender.send(mail);
System.out.println("success");
}
}
MimeMessage mail = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mail, true);//true用来打开multipart模式,添加图片或附件
helper.setTo("573215750@qq.com");
helper.setFrom("573215750@qq.com");
helper.setSubject("test mail");
helper.setText("<html><head></head><body>"
+ "<h1>hello!!spring html Mail</h1>"
+ "</body></html>"
, true);
helper.setText("<html><head></head><body>"
+ "<h1>hello!!spring html Mail</h1>"
+ "<img src=\"cid:image\" />"
+ "</body></html>"
, true);
FileSystemResource image = new FileSystemResource(new File("d:/test.jpg"));
helper.addInline("image", image);
helper.setText("<html><head></head><body>"
+ "<h1>hello!!spring html Mail</h1>"
+ "</body></html>"
, true);
FileSystemResource image = new FileSystemResource(new File("d:/test.jpg"));
helper.addAttachment("test.jpg", image);
<html>
<head></head>
<body>
<p>test freemarker template, welcome ${username}</p>
<img src="cid:image" />
</body>
</html>
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
package com.xmyself.mail;
import java.util.Map;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FreemarkerParser {
public String toHtmlString(String name, Map<String, String> data) {
@SuppressWarnings("deprecation")
Configuration config = new Configuration();
config.setClassForTemplateLoading(this.getClass(), "/templates/");
try {
Template template = config.getTemplate(name);
return FreeMarkerTemplateUtils.processTemplateIntoString(template, data);
} catch (Exception e) {
e.printStackTrace();
}
return "fail";
}
}
Map<String, String> data = new HashMap<String, String>();
data.put("username", "chengyi");
String text = new FreemarkerParser().toHtmlString("test.ftl", data);
helper.setText(text, true);
FileSystemResource image = new FileSystemResource(new File("d:/test.jpg"));
helper.addInline("image", image);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有