springboot 邮箱 java

背景

  • 无聊时,找邮箱来学习,无意间发现了一些问题,借此分享下,先看到代码,在看我的备注。你便知道你的问题出现在哪里了。

实现

  • 新建springboot项目
  • pom.xml中加入邮箱依赖
<!-- 邮箱依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 邮箱依赖 -->
  • 对邮箱在application.properties中进行相应的配置
spring.mail.host=smtp.163.com //网易邮箱服务器
spring.mail.username=发送人邮箱  //发送人邮箱
spring.mail.password= 发送人邮箱的授权码 //发送人邮箱的授权码
spring.mail.port=25 //网易邮箱端口
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
  • 测试代码
package com.example.demo;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestmailApplicationTests {

    @Test
    public void contextLoads() {
    }


     @Autowired
        private JavaMailSenderImpl mailSender;

        /**
         * 发送包含简单文本的邮件
         */
        @Test
        public void sendTxtMail() {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            // 设置收件人,寄件人
            simpleMailMessage.setTo(new String[] {"收件人邮箱"});//收件人邮箱
            simpleMailMessage.setFrom("发送者邮箱");//发送者邮箱
            simpleMailMessage.setSubject("Spring Boot Mail 邮件测试【文本】");
            simpleMailMessage.setText("这里是一段简单文本。");
            // 发送邮件
            mailSender.send(simpleMailMessage);
            System.err.println("邮件已发送");
        }

        /**
         * 发送包含内嵌图片的邮件 ,图片在正文中
         * @throws Exception
         */
        @Test
        public void sendAttachedImageMail() throws Exception {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            // multipart模式
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        simpleMailMessage.setTo(new String[] {"收件人邮箱"});//收件人邮箱
            simpleMailMessage.setFrom("发送者邮箱");//发送者邮箱
            mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【图片】");

            StringBuilder sb = new StringBuilder();
            sb.append("<html><head></head>");
            sb.append("<body><h1>spring 邮件测试</h1><p>hello!this is spring mail test。</p>");
            // cid为固定写法,imageId指定一个标识
            sb.append("<img src=\"cid:imageId\"/></body>");
            sb.append("</html>");

            // 启用html
            mimeMessageHelper.setText(sb.toString(), true);

            // 设置imageId
            FileSystemResource img = new FileSystemResource(new File("C:\\Users\\LMS\\Desktop\\img\\0202.jpg"));
            mimeMessageHelper.addInline("imageId", img);

            // 发送邮件
            mailSender.send(mimeMessage);
            System.err.println("带图片的html邮件已发送");
        }

        /**
         * 发送包含附件的邮件
         * @throws Exception
         */
        @Test
        public void sendAttendedFileMail() throws Exception {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            // multipart模式
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
          simpleMailMessage.setTo(new String[] {"收件人邮箱"});//收件人邮箱
            simpleMailMessage.setFrom("发送者邮箱");//发送者邮箱
            mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【附件】");

            StringBuilder sb = new StringBuilder();
            sb.append("<html><head></head>");
            sb.append("<body><h1>spring 邮件测试</h1><p>hello!this is spring mail test。</p></body>");
            sb.append("</html>");

            // 启用html
            mimeMessageHelper.setText(sb.toString(), true);
            // 设置附件

            FileSystemResource img = new FileSystemResource(new File("C:\\Users\\LMS\\Desktop\\img\\0101.jpg"));
        mimeMessageHelper.addAttachment("image.jpg", img);

            // 发送邮件
            mailSender.send(mimeMessage);

            System.err.println("带附件的邮件已发送");
        }

}
  • 启动类
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestmailApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestmailApplication.class, args);
    }
}
  • 控制台输出
邮件已发送
带图片的html邮件已发送
带附件的邮件已发送
  • 请在邮箱中查看你的邮件信息

备注

  • 发送邮箱时,会遇到部分问题,原因和错误大致如下

    • 535:授权没有通过,这里主要是在配置邮箱的时候,没有正确的输入你的邮箱和密码。这里的密码指的是你邮箱的授权码,不是邮箱登录面。
    • 这个授权码的获得请在网易邮箱的设置–常规设置==客户端授权密码中进行设置
    • 网易的邮箱授权码,不用发送短信,相比于腾讯节约了一毛钱
    • 553:你的邮箱用户名和密码没有写正确。
  • 如果想要发送rar文件,请修改部分文件格式即可,如上所示

参考文章:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章