MimeMessageHelper的基本用法
博客分类: Spring 发邮件
企业应用Spring邮件MimeMessageHelper
org.springframework.mail.javamail.MimeMessageHelper是处理JavaMail比较顺手的组件之一,可以让你摆脱繁复的JavaMail API
接口类:
Java代码 收藏代码
package com.mail.service;
import javax.mail.MessagingException;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 12-6-12
* Time: 下午3:37
* To change this template use File | Settings | File Templates.
*/
public interface OrderManager {
void placeOrder() throws MessagingException;
}
实现类:
Java代码 收藏代码
package com.mail.sender;
import com.mail.service.OrderManager;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.lang.management.MonitorInfo;
/** MimeMessageHelper的基本用法
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 12-6-13
* Time: 上午11:19
* To change this template use File | Settings | File Templates.
*/
public class MimeHelper implements OrderManager {
private JavaMailSender javaMailSender;
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void placeOrder() {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
try {
helper.setTo("
[email protected]");//toXXX邮箱
helper.setFrom("
[email protected]"); //from XXX邮箱
helper.setSubject("helper");
helper.setText("hello");
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
配置:
注入JavamailSender对象
Xml代码 收藏代码
mail.smtp.auth=true
注入MimeHelper对象
Xml代码 收藏代码
测试一下,如果没错就说明成功了