需要的jar包:activation.jar和mail.jar
下载地址:https://download.youkuaiyun.com/download/ly_linyuan/10358216
我这里用用户注册成功后发送邮件提示注册成功为案例
1.mail.properties的配置:
2.spring整合mail的配置文件spring-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<description>JavaMail配置文件</description>
<!-- 加载mail.properties文件 -->
<context:property-placeholder location="classpath*:mail.properties" ignore-unresolvable="true" />
<!-- 配置一个简单邮件对象 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.from}"></property>
</bean>
<!-- 邮件的发送对象 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}"></property>
<property name="username" value="${mail.username}"></property>
<property name="password" value="${mail.password}"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 邮件发送相关的配置信息 -->
<property name="javaMailProperties" >
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.timeout">0</prop>
</props>
</property>
</bean>
</beans>
3.后台注册成功后发送邮件的代码
引入JavaMailSender注解:
@Autowired
private JavaMailSender mailSender;
/**
* 注册
* @return
*/
@RequestMapping("/register/do")
public String doRegister(User user,HttpSession session) {
User u = new User();
u.setTelphone(user.getTelphone());
u.setPassword(user.getPassword());
u.setUsername(user.getUsername());
u.setEmail(user.getEmail());
//保存注册用户的信息
Integer number = userService.insert(u);
if (number != null) {
//注册成功,向用户邮箱发送邮件通知
final String mail = user.getEmail();
final String name = user.getUsername();
//发送邮件
Thread th = new Thread(new Runnable() {
public void run() {
try {
//获取mimeMessage
MimeMessage message = mailSender.createMimeMessage();
//头
//邮件头里邮件发送人地址
message.setFrom(new InternetAddress("lymacaiqin@163.com"));
//邮件收信地址和收信类型
message.setRecipients(Message.RecipientType.TO, mail);
//文件标头
message.setSubject("恭喜"+name+",注册成功!");
//以下部分是扩展跟注册成功提示无关
//两个MimeBodyPart通过MimeMultipart联系一起是有关系的联系在一起
//文本部分
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("aaa<img src='cid:mm'/>aaa", "text/html");
//图片:加载磁盘文件用到了JAF框架
MimeBodyPart imagePart = new MimeBodyPart();
//JAF 框架,要嵌入图片部分
DataHandler dh = new DataHandler(new FileDataSource("E:/图片/背景图/mayun.jpg"));
imagePart.setDataHandler(dh);//JAF会自动探测文件的MIME类型
//图片显示的时候是 src="cid:mm",在这里指明
imagePart.setContentID("mm");
//附件
MimeBodyPart attachmentPart = new MimeBodyPart();
dh = new DataHandler(new FileDataSource("E:/入党材料/ueditor.rar"));
//至关重要的一步,设置邮件中附件的名称,不设置附件的话名称为*******.bin
attachmentPart.setFileName(MimeUtility.encodeText("ueditor.rar"));
attachmentPart.setDataHandler(dh);
//建立关系,图片嵌入关联起来
MimeMultipart mmpart1 = new MimeMultipart();
mmpart1.addBodyPart(textPart);
mmpart1.addBodyPart(imagePart);
mmpart1.setSubType("related");//有关系的
//嵌入图片和文字关联bodyPart
MimeBodyPart textImagePart = new MimeBodyPart();
textImagePart.setContent(mmpart1);
//和附近关系起来
MimeMultipart mmpart = new MimeMultipart();
mmpart.addBodyPart(textImagePart);
mmpart.addBodyPart(attachmentPart);
mmpart.setSubType("mixed");
//放进message里
message.setContent(mmpart);
//邮件内容包含在一个文件夹里
message.saveChanges();
//发送邮件
mailSender.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
});
th.start();
return "redirect:/";
}else{
return "user/register";
}
}
4.运行效果
发送方:
接收方:
邮件内容: