spingboot之Java邮件发送

注意: 该项目的工具类可以直接应用于项目

1、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rocketmq</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--基础 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- activemq -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.4</version>
        </dependency>
        <!-- spring-activemq -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

        <!-- rocketmq -->
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-client</artifactId>
            <version>4.2.0</version>
        </dependency>
        <!--  fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.31</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

注意:pom.xml包含了一些其他的jar依赖,其实是不需要的,只需要有:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
  </dependency>

2、application.properties

注意:这个配置文件只是针对QQ邮箱,如果是163或者其他企业邮箱,需要根据需要配置

server.port=8080
spring.mail.host=smtp.qq.com
spring.mail.username=自己的qq邮箱
spring.mail.password=密码,不是登陆密码,是开启smtp的授权码
spring.mail.default-encoding=UTF-8
spring.mail.port=465   
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true

3、SendMailUtil.java

package com.rocketmq.demo.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class SendMailUtil {
    @Autowired
    JavaMailSender mailSender;

    /**
     * 发送文本消息
     * @param senderMail    发送者邮箱
     * @param receiveMail   接受者邮箱
     * @param subject   主题
     * @param text  主体内容
     * @return
     */
    public  String  sendMail(String senderMail,String receiveMail,String subject,String text){
        try {
            final MimeMessage message = this.mailSender.createMimeMessage();
            final MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(senderMail);//发送者邮箱
            helper.setTo(receiveMail);//接收者邮箱
            helper.setSubject(subject);
            helper.setText(text);
            this.mailSender.send(message);
            return "sucesss";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "error";
        }
    }


    /**
     * 带附件发送邮件----可多个附件 图片,doc都可以发送
     * @param senderMail  发送者邮箱
     * @param receiveMail 接受者邮箱
     * @param subject   主题
     * @param text      主体内容
     * @param fileArray    附件路径
     * @return
     */
    public  String  sendMailFile(String senderMail,String receiveMail,String subject,String text,String[] fileArray) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(senderMail);
            helper.setTo(receiveMail);
            helper.setSubject(subject);
            helper.setText(text);
            //验证文件数据是否为空
            if (null != fileArray) {
                FileSystemResource file = null;
                for (int i = 0; i < fileArray.length; i++) {
                    //添加附件
                    file = new FileSystemResource(fileArray[i]);
                    helper.addAttachment(fileArray[i].substring(fileArray[i].lastIndexOf(File.separator)), file);
                }
            }
            mailSender.send(message);
            System.out.println("带附件的邮件发送成功");
            return "sucesss";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送带附件的邮件失败");
            return "error";
        }
    }
}

4、mailController.java

package com.rocketmq.demo.controller;

import com.rocketmq.demo.Utils.SendMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mail")
public class mailController {
    @Autowired
    SendMailUtil sendMailUtil;

    /**
     * 发送文本消息,不带附件
     * @return
     */
    @ResponseBody
    @RequestMapping("/send")
    public Object sendEmail() {
        String senderMial = "发送者@qq.com";
        String receiveMail = "接收者@qq.com";
        String subject = "测试主题";
        String text = "测试内容";
        return sendMailUtil.sendMail(senderMial,receiveMail,subject,text);
    }

    @GetMapping("/sendFile")
    public String sendAttachmentsMail() {
        String senderMial = "发送者@qq.com";
        String receiveMail = "接收者@qq.com";
        String subject = "测试主题";
        String text = "测试内容";
        String[] path={"C:\Users\yangwj\Desktop\公司\正式员工详细信息表.xls"};
        return  sendMailUtil.sendMailFile(senderMial,receiveMail,subject,text,path);

    }


}

总结:

  setFrom:指的是发送者的邮箱,要和配置文件的一致。

具体代码:https://github.com/812406210/RocketMQ-and-mail.git

原文地址:https://www.cnblogs.com/ywjfx/p/10767797.html