JavaMail 与 Spring 集成开发

一、什么是JavaMail

JavaMail是提供给开发人员在应用程序中实现邮件发送和接受功能而提供的一套标准开发类库,支持常用的邮件协议,如SMTP/POP3.IMAP,开发人员使用JavaMail编写邮件程序时,无需考虑底层的通信细节(Socket),JavaMail也提供了能够创建出各种复杂MIME格式的邮件内容API.使用JavaMail,我们可以实现类似OutLook、FoxMail的软件。

二、JavaMail的基本概述

  1. 邮件开发的相关协议
    SMTP:Simple Message Transfer Protocal 发送协议 默认端口:25
    POP: Post Office Protocal 邮局协议。POP3这个版本用的最多,接受协议 默认端口:110
  2. 邮件发送接受的过程分析
    这里写图片描述

三、邮件开发的准备工作

  1. 申请邮箱
    我这里用到是QQ邮箱,需要开通邮箱的SMTP/POP服务:
    这里写图片描述

这里写图片描述


这里写图片描述


2.引入JavaMail
在maven工程中引入相关的依赖

<!-- 发送邮件的两个jar包 -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.5.6</version>
    </dependency>

4.Spring对javaMail的支持

       Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和其对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。 包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息。

       为了使用JavaMail中的一些特色, 比如MIME类型的信件, spring提供了MailSender的一个子接口, 即org.springframework.mail.javamail.JavaMailSender。Spring还提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator, 用于准备JavaMail的MIME信件。
这里简单的介绍了如何使用spring发送各种形式的邮件以及配置。

这里写图片描述

1、在src目录下建立mail.properties文件里边包含一下内容

  • mail.properties
#QQ邮件服务地址
mail.host=smtp.qq.com
#用户名
mail.username=190xxxx981@qq.com
#密码(授权码)
mail.password=xxddgdgsddeejd
#端口,这里添加587即可
mail.port=587
#邮件发送人
mail.from=190xxxx981@qq.com

配置信息这里使用587端口,默认端口会报如下错误:
这里写图片描述
2、使用spring配置

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 读取properties配置文件 -->
    <context:property-placeholder location="classpath:mail.properties"/>

    <!-- 配置一个简单邮件对象 -->
    <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.host}"></property>
         <property name="port" value="${mail.port}"/>
         <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">true</prop>
                <!--   启动调试开关 -->
                  <prop key="mail.debug">true</prop>
                  <!-- 设置发送延迟 -->
                  <prop key="mail.smtp.timeout">0</prop>
            </props>
         </property>
    </bean>

</beans>
  • 发送简单邮件

public class Demo
{
    static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
    static MailSender sender = (MailSender) ctx.getBean("mailSender");
    static SimpleMailMessage mailMessage = (SimpleMailMessage) ctx.getBean("mailMessage");

    @Test
    public void test1(){
        mailMessage.setSubject("你好,这是邮件主题");
        mailMessage.setText("这是邮件的正文");
        mailMessage.setTo("ponyii@qq.com");
        sender.send(mailMessage);

    }
}

这里写图片描述


这里写图片描述


  • 发送带图片的邮件,内嵌HTML形式

public class Demo
{
    static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");

    @Test
    public void test() throws MessagingException{

        JavaMailSenderImpl sender = (JavaMailSenderImpl) ctx.getBean("mailSender");
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
        //发送邮件方
        messageHelper.setFrom("xxxxxxxxx1@qq.com");
        //接受邮件方
        messageHelper.setTo("1xxxxxxx40@qq.com");

        messageHelper.setSubject("小哥哥,来玩呀");
        //true表示启动html格式的邮件
        messageHelper.setText("<html><head></head><body><h1>hello!!baby </h1>"
                    +"<a href=http://www.baidu.com>屠龙宝剑,点击就送</a>"   + "<img src=cid:image/></body></html>",true);

        //添加图片
        FileSystemResource resource = new FileSystemResource(new File("C:/Users/Administrator/Desktop/timg.jpg"));
        //跟cid一致
        messageHelper.addInline("image", resource);


        sender.send(message);
    }
}

这里写图片描述

  • 发送带附件的邮件,内嵌HTML形式
public class Demo
{
    static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");

    @Test
    public void test() throws MessagingException{

        JavaMailSenderImpl sender = (JavaMailSenderImpl) ctx.getBean("mailSender");
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
        //发送邮件方
        messageHelper.setFrom("ponyii@qq.com");
        //接受邮件方
        messageHelper.setTo("60xxx32@qq.com");

        messageHelper.setSubject("小哥哥,来玩呀");
        //true表示启动html格式的邮件
        messageHelper.setText("<html><head></head><body><h1>hello!!baby </h1>"
                    +"<a href=http://www.baidu.com>屠龙宝剑,点击就送</a><br/>"  
                + "<img src=cid:image/></body></html><br/>",true);

        //添加图片
        FileSystemResource resource = new FileSystemResource(new File("C:/Users/Administrator/Desktop/宋智孝.jpg"));
        //跟cid一致
        messageHelper.addInline("image", resource);
        //发送时带附件
        FileSystemResource zipResource = new FileSystemResource(new File("C:/Users/Administrator/Desktop/timg.jpg"));
        messageHelper.addAttachment("更多美图.jpg", zipResource);

        sender.send(message);
    }
}

这里写图片描述


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值