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文件,請修改部分文件格式即可,如上所示

參考文章:

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