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>

注意: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

时间: 2024-10-10 06:05:05

spingboot之Java邮件发送的相关文章

java-基于JavaMail的Java邮件发送

1.基于JavaMail的Java邮件发送:简单邮件发送 2.基于JavaMail的Java邮件发送:复杂邮件发送

Java邮件发送与屏幕截屏

前几天七夕情人节孤独寂寞的程序猿闲来没事,花了一两个小时写了个小Demo主要实现Java的Mail发送功能和桌面截屏功能. 首先让我们先看看Java sendMail邮件发送和桌面屏幕截屏功能是怎么实现的基础知识. 一.Java  SendMail邮件发送 首先让我们来看看邮件发送的原理图: JavaMail 是一套sun 提供开发邮件收发程序API,JavaMail编写程序就是邮件客户端程序(和outlook.foxmail功能类似) * JavaMail开发需要类库 javamail API

新版本Java邮件发送类

之前曾经做过几个邮件发送类,有兴趣可以查阅前面的帖子. 使用过程中,发现一些不便之处,并作出了改进,将改进后的版本发布如下: 基类及其附属类共三个:(下载地址:http://pan.baidu.com/s/1bn1VkUN) import java.io.File; import java.util.Date; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; impo

Java邮件发送

package com.expai.test; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Session; i

JAVA邮件发送的简单实现

JAVA邮件发送的简单实现 JAVA邮件发送的简单实现 JAVA MAIL是利用现有的邮件账户发送邮件的工具,比如说,我在网易注册一个邮箱账户,通过JAVA Mail的操控,我可以不亲自登录网易邮箱,让程序自动的使用网易邮箱发送邮件.这一机制被广泛的用在注册激活和垃圾邮件的发送等方面. JavaMail可以到http://www.oracle.com/technetwork/java/javamail/index-138643.html进行下载,并将mail.jar添加到classpath即可.

java邮件发送(含附件)

1. [代码]java邮件发送(含附件)疯狂的IT人站长整理的:利用Java发送邮件(含附件)的例子:1.邮件发送的配置propertity文件内容如下:(utils.properties文件放在src下面)emailsmtp=smtp.qq.com[email protected]emailpass=******2.读取配置文件的类文件(ReadPropertity.java)import java.io.IOException;import java.util.Properties;publ

基于JavaMail的Java邮件发送:简单邮件发送

http://blog.csdn.net/xietansheng/article/details/51673073 http://www.cnblogs.com/codeplus/archive/2011/10/30/2229391.html http://blog.csdn.net/ghsau/article/details/17839983 ******************** 电子邮件的应用非常广泛,例如在某网站注册了一个账户,自动发送一封欢迎邮件,通过邮件找回密码,自动批量发送活动信

java邮件发送(以163邮箱为例)

1.首先应该开通163邮箱的smtp和pop3,得到授权码 2.其次建立一个web项目,否则需要倒jar包mail.jar 3.创建一个类 4.注意:邮件内容必须为正式话语,否则系统会认为是垃圾邮件而拒收,报错541DT public static void main(String[] args) throws MessagingException { Properties prop=new Properties(); prop.put("mail.host","smtp.1

JAVA 邮件发送工具类

1.封装邮件接收地址MODEL和邮件信息MODEL package com.sicdt.jnzxgzc.common.mail.model; import java.io.Serializable; /** * * <br>类 名: MailAddress * <br>描 述: InternetAddress的封装类 * <br>作 者: shizhenwei * <br>创 建: 2017年8月4日 * <br>版 本: v1.0.0 * &l