TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
1,在spring的配置文件中,配置javaMailSenderImpl,如下:
<!--邮件服务器-->
<bean id="mailSender">
<property name="host">
<value>${mail.smtp.host}</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
</props>
</property>
<property name="username">
<value>${mail.user}</value>
</property>
<property name="password">
<value>${mail.pwd}</value>
</property>
</bean>
其中,${}中的值从mail.properties文件中读取,mail.properties文件中的配置:
mail.smtp.host=邮箱服务器
mail.user=用户名
mail.pwd=密码
mail.from=发送者的邮箱
mail.replyTo=回复者的邮箱
mail.smtp.timeout=25000
mail.smtp.auth=true
mail.ad.host=ad服务器
该properties文件在spring.xml文件中的配置如下:
<!-- 加载配置文件 -->
<bean>
<property name="locations">
<list>
<value>/WEB-INF/springcfg/dataSource.properties</value>
<value>/WEB-INF/springcfg/mail.properties</value>
</list>
</property>
</bean>
2,创建一个实体类Mail.java,封装邮件的属性:
import java.io.File;
import java.io.Serializable;
import java.util.List;
public class Email implements Serializable {
private static final long serialVersionUID = 9063903350324510652L;
/** 用户组:可以按用户组来批量发送邮件 **/
/** 待用属性 **/
/** 收件人 **/
private List<String> address;
/** 暗送 **/
private String bcc;
/** 抄送给 **/
private List<String> cc;
/** 邮件主题 **/
private String subject;
/** 邮件内容 **/
private String content;
/** 附件 **/
private List<File> annexFile;
public List<String> getAddress() {
return address;
}
public void setAddress(List<String> address) {
this.address = address;
}
public List<String> getCc() {
return cc;
}
public void setCc(List<String> cc) {
this.cc = cc;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<File> getAnnexFile() {
return annexFile;
}
public void setAnnexFile(List<File> annexFile) {
this.annexFile = annexFile;
}
public String getBcc() {
return bcc;
}
public void setBcc(String bcc) {
this.bcc = bcc;
}
}
3,创建发送邮件的工具类MailUtil.java:
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class MailUtil {
private JavaMailSender mailSender;
private TaskExecutor taskExecutor;
private Properties mailProperty;
private StringBuffer message = new StringBuffer();
// public static ResourceBundle getProperties() {
// ResourceBundle rb = ResourceBundle.getBundle("mail");
// return rb;
// }
public void setMailProperty(Properties mailProperty) {
this.mailProperty = mailProperty;
}
/**
* 一对多发送邮件
* @param mail
* @param subject
* @param content
* @param copyMail
* @return
*/
public boolean sendMail(List<String> mailList, String subject, String content,List<String> copyMailList){
boolean flag = false;
try {
Email email = new Email();
email.setAddress(mailList);
if(copyMailList!= null){
email.setCc(copyMailList);
}
email.setContent(content);
email.setSubject(subject);
sendMail(email);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 发送邮件
* @param email
* @throws Exception
*/
public void sendMail(Email email) throws Exception {
if (email.getAddress() == null || email.getAddress().size() == 0) {
this.message.append("没有收件人");
return;
}
if (email.getAddress().size() > 5) {// 收件人大于5封时,采用异步发送
sendMailByAsynchronousMode(email);
this.message.append("收件人过多,正在采用异步方式发送...<br/>");
} else {
sendMailBySynchronizationMode(email);
this.message.append("正在同步方式发送邮件...<br/>");
}
}
/**
* 异步发送
*
*/
public void sendMailByAsynchronousMode(final Email email) {
taskExecutor.execute(new Runnable() {
public void run() {
try {
sendMailBySynchronizationMode(email);
} catch (Exception e) {
}
}
});
}
/**
* 同步发送
*
* @throws IOException
*/
public void sendMailBySynchronizationMode(Email email) throws Exception {
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mime, true, "utf-8");
helper.setFrom(this.mailProperty.getProperty("mail.from"));// 发件人
List<String> list = email.getAddress();
String[] addrs = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
addrs[i] = list.get(i);
}
helper.setTo(addrs);// 收件人
// helper.setBcc(email.getBcc());// 暗送
List<String> ccList = email.getCc();
if(ccList!=null ){
String[] cc = new String[ccList.size()];
for (int i = 0; i < ccList.size(); i++) {
cc[i] = ccList.get(i);
}
helper.setCc(cc);// 抄送
}
// 内嵌资源,这种功能很少用,因为大部分资源都在网上,只需在邮件正文中给个URL就足够了.
// helper.addInline("logo", new ClassPathResource("logo.gif")); //处理附件
if(email.getAnnexFile()!=null){
for (File file : email.getAnnexFile()) {
if (file == null) {
continue;
}
helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
}
}
helper.setReplyTo(this.mailProperty.getProperty("mail.replyTo"));// 回复到
helper.setSubject(email.getSubject());// 邮件主题
helper.setText(email.getContent(), true);// true表示设定HTML格式
mailSender.send(mime);
}
public JavaMailSender getMailSender() {
return mailSender;
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public TaskExecutor getTaskExecutor() {
return taskExecutor;
}
public void setTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
}
4,在spring配置文件中配置MailUtil:
<!-- Email -->
<bean id="mailUtil" scope="prototype">
<property name="mailSender" ref="mailSender" />
<property name="taskExecutor" ref="taskExecutor" />
<property name="mailProperty">
<props>
<prop key="mail.from">${mail.from}</prop>
<prop key="mail.replyTo">${mail.replyTo}</prop>
</props>
</property>
</bean>
5,此时便可以在service层设置MailUtil的getter和setter方法然后使用MailUtil的对象来发送邮件了。。。。
|
|