环境: 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>
分享到:
相关推荐
使用Spring Boot 开发支持多附件邮件发送微服务平台代码
这个java project的源代码 博文链接:https://kukuqiu.iteye.com/blog/161771
java 发送邮件 spring发送邮件Mail
spring发送 email 发送html格式数据,普通数据,附件,图片等邮件
本例为完整的采用spring 发送带 附件的邮件。 下载后需要改动的地方: 1.在mail.properties中将接收者邮箱地址(username),邮箱密码换成真实的。 2.在SpringJavaMailDemo类中把发送者和接收者邮箱地址换成真实地址...
Spring mail发送邮件实例 Spring mail发送邮件实例
3、Spring Boot 发送邮件(文本格式的邮件、发送HTML格式的邮件、发送带附件 的邮件、发送带静态资源的邮件) 个人觉得Springboot的开发简单的归纳为三步jar包引入,配置,应用。 (一)简单使用 1)JSP的使用...
SpringBoot中整合Mail实现发送带附件的邮件示例代码SpringBoot中整合Mail实现发送带附件的邮件示例代码
使用Spring mail发送邮件的完整实例,包含代码与Spring mail的jar,可直接运行与使用。相关学习文档参考博客《Spring Mail邮件发送 http://blog.csdn.net/chenxiang0207/article/details/8256528》。
spring boot发送普通文本邮件/HTML邮件/附件邮件/图片邮件完整代码.zip
NULL 博文链接:https://longjuanfeng885.iteye.com/blog/1895577
spring + mail + 发送邮件 demo
Spring邮件发送服务(java邮件发送),支持多附件、多人发送/抄送/暗送
Spring实现各种邮件的发送 简单配置就可发送邮件
NULL 博文链接:https://chaoyi.iteye.com/blog/2171158
spring boot 邮件发送,亲测可用 #字符集 spring.mail.default-encoding=UTF-8 #电子邮件地址 spring.mail.host=smtp.126.com #Application spring.application.name=SEND-MAIL #授权密码 spring.mail.password=...
Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。 包里还包含一棵以...