package net.bolue.mail;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
public class SpringMailSender {
// Spring的邮件工具类,实现了MailSender和JavaMailSender接口
private JavaMailSenderImpl mailSender;
public SpringMailSender() {
// 初始化JavaMailSenderImpl,当然推荐在spring配置文件中配置,这里是为了简单
mailSender = new JavaMailSenderImpl();
// 设置参数
mailSender.setHost("smtp.qq.com");
mailSender.setUsername("
[email protected]");
mailSender.setPassword("asterisks");
}
/**
* 带附件的邮件发送
*
* @throws MessagingException
*/
public void attachedSend() throws MessagingException {
//使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容
MimeMessage msg = mailSender.createMimeMessage();
//创建MimeMessageHelper对象,处理MimeMessage的辅助类
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
//使用辅助类MimeMessage设定参数
helper.setFrom(mailSender.getUsername());
helper.setTo("
[email protected]");
helper.setSubject("Hello Attachment");
helper.setText("This is a mail with attachment");
//加载文件资源,作为附件
ClassPathResource file = new ClassPathResource("Chrysanthemum.jpg");
//加入附件
helper.addAttachment("attachment.jpg", file);
//发送邮件
mailSender.send(msg);
}
/**发送富文本邮件
* @throws MessagingException
*/
public void richContentSend() throws MessagingException {
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setFrom(mailSender.getUsername());
helper.setTo("
[email protected]");
helper.setSubject("Rich content mail");
//第二个参数true,表示text的内容为html,然后注意<img/>标签,src='cid:file','cid'是contentId的缩写,'file'是一个标记,需要在后面的代码中调用MimeMessageHelper的addInline方法替代成文件
helper.setText(
"<body><p>Hello Html Email</p><img src='cid:file'/></body>",
true);
FileSystemResource file = new FileSystemResource(
"C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
helper.addInline("file", file);
mailSender.send(msg);
}
/*
public SpringMailSender(String className) {
// Velocity的参数,通过VelocityEngineFactoryBean创建VelocityEngine,也是推荐在配置文件中配置的
Properties props = System.getProperties();
props.put("resource.loader", "class");
props
.put("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
VelocityEngineFactoryBean v = new VelocityEngineFactoryBean();
v.setVelocityProperties(props);
try {
velocityEngine = v.createVelocityEngine();
} catch (VelocityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void templateSend() throws MessagingException {
// 声明Map对象,并填入用来填充模板文件的键值对
Map<String, String> model = new HashMap<String, String>();
model.put("user", "MZULE");
model.put("content", "Hello");
// Spring提供的VelocityEngineUtils将模板进行数据填充,并转换成普通的String对象
String emailText = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "index.vm", model);
// 和上面一样的发送邮件的工作
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setFrom(mailSender.getUsername());
helper.setTo("
[email protected]");
helper.setSubject("Rich content mail");
helper.setText(emailText, true);
mailSender.send(msg);
}
*/
}