javamail的使用

需要的jar包:activation.jar和mail.jar
下载地址:https://download.csdn.net/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.运行效果
这里写图片描述
这里写图片描述
发送方:
这里写图片描述
接收方:
这里写图片描述

邮件内容:
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值