环境: Eclipse
DNS: mail.company.com 192.168.x.x
程序
Main
MailSender
applicationContext.xml
VM_global_library.vm
附件
attachment.txt
inline.jpg
类库
commons-collections-3.1.jar
commons-logging-1.0.4.jar
mail.jar // java mail
activation.jar
spring-beans.jar // bean
spring-context.jar // 配置类
spring-core.jar // 核心类库
spring-support.jar // spring mail
velocity-1.4.jar // 模板引擎
import java.util.HashMap;
import java.util.Map;
import javax.mail.internet.MimeMessage;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;
/**
* Spring mail 使用多个账号发送带有附件的HTML邮件
*
* 1.JavaMail的API和Spring的邮件抽象层的对比
* 优点:Spring的邮件抽象层简化了代码量,并能充分利用IOC功能.
* 缺点:要使用部分Spring API,使程序与第三方框架耦合.
*
* 2.Velocity允许我们在模版中设定变量,然后在运行时,动态的将数据插入到模版中,替换这些变量。
* @author Administrator
*
* BUG:
* 附件中文乱码
*/
public class Main {
public static void main(String[] args) {
/* 1.Spring IOC 获得 Bean */
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
JavaMailSenderImpl sender = (JavaMailSenderImpl) context.getBean("mailSender"); // 可以发送带有附件的邮件
VelocityEngine velocityEngine = (VelocityEngine)context.getBean("velocityEngine");
try {
/* 2.邮件内容(VelocityEngine) */
String templateLocation = "VM_global_library.vm"; // 默认模板(VelocityModel)
Map<String, String> model = new HashMap<String, String>();
model.put("CONTENT", "内容");
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateLocation, model);
/* 3.发送邮件(JavaMailSender) */
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");// 处理中文编码
// 信息
helper.setSubject("主题"); // 主题
helper.setFrom(sender.getUsername()); // 发件人
helper.setTo("you@mail.com"); // 收件人
helper.setText(text, true); // 内容(HTML)
String contentId = "inline";
Resource resource = new ClassPathResource("inline.jpg");
String attachmentFilename = "attachment";
InputStreamSource inputStreamSource = new ClassPathResource("attachment.txt");
helper.addInline(contentId, resource); // 附件(行内)
helper.addAttachment(attachmentFilename, inputStreamSource); // 附件
// 发送
sender.send(message);
// 发送成功
} catch (Exception e) {
// 没有发送成功
}
}
}
import java.util.ArrayList;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class MailSender extends JavaMailSenderImpl implements
JavaMailSender {
// Accounts
private ArrayList<String> userNameList;
private ArrayList<String> passwordList;
private int currentMailId = 0;
@Override
protected void doSend(MimeMessage[] mimeMessage, Object[] object) throws MailException {
super.doSend(mimeMessage, object);
// Next Accounts
currentMailId = (currentMailId + 1) % userNameList.size();
super.setUsername(userNameList.get(currentMailId));
super.setPassword(passwordList.get(currentMailId));
}
@Override
public void setUsername(String username) {
if (userNameList == null)
userNameList = new ArrayList<String>();
// split usernmae in ,
String[] userNames = username.split(",");
if (userNames != null) {
for (String user : userNames) {
userNameList.add(user);
}
}
// before send super.setXxx() // accounts
super.setUsername(userNameList.get(currentMailId));
}
@Override
public void setPassword(String password) {
if (passwordList == null)
passwordList = new ArrayList<String>();
// split password in ,
String[] passwords = password.split(",");
if (passwords != null) {
for (String pw : passwords) {
passwordList.add(pw);
}
}
// before send super.setXxx() // accounts
super.setPassword(passwordList.get(currentMailId));
}
}
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- Spring Mail --> <bean id="mailSender" class="MailSender"> <!-- 多个账号设置 --> <property name="username" value="a@mail.com,b@mail.com,c@mail.com"/> <property name="password" value="a,b,c"/> <!-- 邮箱属性设置 --> <property name="javaMailProperties"> <props> <prop key="mail.smtp.host">mail.company.com</prop> <!-- 简单邮件传输协议许可 --> <prop key="mail.smtp.auth">true</prop> <prop key="mail.debug">true</prop> <prop key="mail.smtp.timeout">20000</prop> <!-- SMTP加密方式 tls --> <prop key="mail.smtp.starttls.enable">false</prop> </props> </property> </bean> <!-- Velocity模版引擎 --> <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="velocityProperties"> <value> resource.loader=class class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader </value> </property> </bean> </beans>