使用-Spring-API-实现邮件发送-乐字节java乐字节架构

42次阅读

共计 3483 个字符,预计需要花费 9 分钟才能阅读完成。

详细 Spring 如何进行邮件发送
本文由乐字节 Java 架构课程独家赞助播出

环境准备

创建 Maven 项目,在 pom.xml 文件中添加依赖

<!-- spring 核心 jar 依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>
<!--spring 上下文环境 支持 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.4.RELEASE</version>
    <scope>test</scope>
</dependency>
<!-- Java Mail 坐标依赖 -->
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

配置邮件发送 bean

在 spring.xml 配置文件中设置邮件发送对应的 bean 标签

<?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">

    <!-- 开启自动化扫描  -->
    <context:component-scan base-package="com.xxxx"/>

    <!-- 邮件发送器(提供了邮件发送接口、透明创建 Java Mail 的 MimeMessage、及邮件发送的配置)-->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.163.com" />
        <property name="port" value="25" />
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="username" value="用户名"></property>
        <property name="password" value="密码"></property>
    </bean>

    <!-- 普通文本邮件对象 -->
    <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
        <property name="from" value="发件人的邮箱地址" />
        <property name="subject" value="spring_mail" />
    </bean>

</beans>

定义接口与实现类

定义接口

package com.xxxx.springMail;

/**
 * 定义接口
 */
public interface OrderManager {void placeOrder();
}

定义实现类

package com.xxxx.springMail;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class SimpleOrderManager implements OrderManager {

    @Resource
    private JavaMailSenderImpl mailSender;
    @Resource
    private SimpleMailMessage templateMessage;

    @Override
    public void placeOrder() {
        // 获取邮件对象
        SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
        // 设置邮件收件人的邮箱地址
        msg.setTo("收件人的邮箱地址");
        // 设置邮件内容
        msg.setText("Hello Spring Mail");
        try{
            // 发送邮件
            this.mailSender.send(msg);
        } catch (MailException ex) {System.err.println(ex.getMessage());
        }
    }
}

邮件发送测试

/**
  *  发送邮件
  * @param args
  */
public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
    SimpleOrderManager simpleOrderManager = 
        (SimpleOrderManager) ac.getBean("simpleOrderManager");
    simpleOrderManager.placeOrder();}

发送附件

/**
  *  发送包含附件的邮件
  * @param args
  * @throws MessagingException
  */
public static void main(String[] args) throws MessagingException {ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    JavaMailSender mailSender= (JavaMailSender) ac.getBean("mailSender");
    MimeMessage message= mailSender.createMimeMessage();
    message.setSubject("spring_mail"); // 邮件主题
    // 创建带有附件的消息帮组类
    MimeMessageHelper helper = new MimeMessageHelper(message,true,"utf-8");
    helper.setTo(new InternetAddress("收件人的邮箱地址"));// 设置接收人
    helper.setText("Thank you for ordering!"); // 邮件内容
    helper.setFrom("发件人的邮箱地址"); // 发件人
    // 设置附件
    File file = new File("C:\\work\\ 邮件附件.txt");
    // 添加附件
    helper.addAttachment(file.getName(), file);
    // 发送邮件
    mailSender.send(message);
}

正文完
 0