當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot高级-任务-邮件任务
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot高级-任务-邮件任务
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
接下來測試郵件任務,在我們開發(fā)中也用的非常多,SpringBoot也加入了相關的場景支持,我們只需要引入spring-boot-starter-mail,加上一些少量的配置,我們就可以發(fā)送郵件,我們來測試一下,我們pom文件中引入依賴<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>郵件的場景啟動器我們在starter里面來查一下https://docs.spring.io/spring-boot/docs/1.5.22.RELEASE/reference/html/using-boot-build-systems.html
#using-boot-starterspring-boot-starter-mail我們把它引入,我們來看一下他的依賴,他引入了javax.mail,我們再來看他的自動配置,自動配置我們自然來到autoconfigure包下,來看跟郵件有關的配置,我們來找到mailorg.springframework.boot.autoconfigure.mail郵件配置這里有一個MailSenderAutoConfiguration,他下面給我們放了一個組件,叫JavaMailSenderImpl,這個組件是用來發(fā)送郵件的,但是郵件要的屬性,@Bean
public JavaMailSenderImpl mailSender() {JavaMailSenderImpl sender = new JavaMailSenderImpl();if (this.session != null) {sender.setSession(this.session);}else {applyProperties(sender);}return sender;
}屬性可以在MailProperties里面配置private final MailProperties properties;這個Properties里面能夠配哪些屬性,比如我們郵件的主機,/*** SMTP server host.*/
private String host;SMTP主機的地址,以及端口/*** SMTP server port.*/
private Integer port;包括用戶名/*** Login user of the SMTP server.*/
private String username;密碼/*** Login password of the SMTP server.*/
private String password;這些郵件發(fā)送的相關配置,特別說郵件發(fā)送
比如zhangsan@qq要給lisi@163發(fā)郵件,不是說他直接跟他交互的,而是說zhangsan@qq,先登錄他自己的郵箱服務器,就要用賬號密碼來登陸,我們要配賬號密碼,登陸進來以后呢,以這個賬號為名,來給lisi發(fā)郵件,lisi發(fā)郵件呢,是QQ郵箱服務器,把zhangsan郵件發(fā)送給163服務器,lisi上線以后呢再從163服務器,獲取他的郵件,是這么一個流程,所以我們郵件發(fā)送需要配置的是,我們發(fā)件人的用戶名,密碼,以及他所在的服務器的地址,我就以QQ郵箱給163發(fā)送為例,我這兒有QQ郵箱,然后我們要給163發(fā)郵件,那我得來配登陸進163郵箱,我在這來配置一下,這個密碼大家注意,QQ郵箱,網(wǎng)易等等,為了安全性起見,填的不是你QQ的密碼,需要來到郵箱設置里面,賬戶里面,首先大家要來開通這些服務,POP3,SMTP,開通了以后呢,這里有第三方登陸郵箱,要有一個授權碼,你要使用郵箱,你就可以來使用授權碼,這樣就能保證安全,我在這填一個授權碼nomhhyejwyztdcdcspring.mail.username=306758218788@qq.com
spring.mail.password=nomhhyejwyztdcdc
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true需要你你手機綁定來發(fā)短信,這個授權碼就是我們要記住的,我們來把這個授權碼填上,包括我們還要來填地址,mail.host我們的主機地址,主機地址填什么呢,填我們smtp服務器的地址,如何收發(fā)郵件,smtp.qq.com,我們的QQ服務器,配置我們就做完了,我們在單元測試里面來測試一下,測試只要注,郵件發(fā)送器就自動注入進來,這怎么發(fā)送郵件呢,mailSender有一個方法,有一個叫send發(fā)送郵件,能傳Mime類型的郵件,能傳簡單的郵件,我先來以簡單的郵件為例,我來new一個SenderMailMessage,先來創(chuàng)建一個簡單郵件,這個簡單郵件我把它傳遞過來,這就發(fā)送了,郵件設置,比如郵件的標題了,寶庫偶郵件的內容,setTo發(fā)給誰,包括這個郵件是誰發(fā)的,發(fā)現(xiàn)這會有報錯,報錯的原因我們來看一下,530,他說需要一個安全的連接,比如SSL,我們連接QQ郵箱的話,是需要安全連接的,我們得需要額外配一些東西,配置什么呢,我們來到MailProperties里面,額外的配置都寫在mail.properties里面,以key value的方式,我們來開啟sslspring.mail.properties.mail.smtp.ssl.enable=true然后我們再來測試,我們看現(xiàn)在能不能發(fā)送成功,我們發(fā)現(xiàn)這一塊郵件已經(jīng)發(fā)送成功,在未讀郵件里面就有一個今晚開會
package com.learn.springboot;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.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootTaskApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testpublic void contextLoads() {SimpleMailMessage message = new SimpleMailMessage();message.setSubject("通知-今晚開會");message.setText("今晚7:30開會");message.setTo("870116654@qq.com");message.setFrom("306758218788@qq.com");mailSender.send(message);}
}
我們這個郵件就發(fā)出來了,沒問題,我們測試的這個呢,只是一個簡單郵件發(fā)送,我們如何發(fā)送一個帶附件的,或者一些HTML頁面的復雜郵件呢,我們在這來測試一下,這個也比較簡單,我們測在第二個方法里面,簡單郵件叫SimpleMailMessage,而復雜郵件要怎么做呢,我們首先第一步,創(chuàng)建一個復雜的消息郵件,怎么創(chuàng)建呢,利用mailSender.createMimeMessage,創(chuàng)建出來,這個MimeMessage呢,就是我們最終要發(fā)送的消息,我們要給他設置一些內容,但是直接用它來設置是不行的,他沒有這個方法的支持,那我們這塊要怎么設置呢,/*** Create a new MimeMessageHelper for the given MimeMessage,* in multipart mode (supporting alternative texts, inline* elements and attachments) if requested.* <p>Consider using the MimeMessageHelper constructor that* takes a multipartMode argument to choose a specific multipart* mode other than MULTIPART_MODE_MIXED_RELATED.* <p>The character encoding for the message will be taken from* the passed-in MimeMessage object, if carried there. Else,* JavaMail's default encoding will be used.* @param mimeMessage MimeMessage to work on* @param multipart whether to create a multipart message that* supports alternative texts, inline elements and attachments* (corresponds to MULTIPART_MODE_MIXED_RELATED)* @throws MessagingException if multipart creation failed* @see #MimeMessageHelper(javax.mail.internet.MimeMessage, int)* @see #getDefaultEncoding(javax.mail.internet.MimeMessage)* @see JavaMailSenderImpl#setDefaultEncoding*/
public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException {this(mimeMessage, multipart, null);
}如果我們要上傳文件,我們就寫成multipart,拿到helper以后,我們使用helper來設置內容,紅色報錯有異常,我還可以把內容設置成HTML片段,加粗的一段文字,就能兼容HTML片段了,比如我要上傳一些文件,那要怎么辦呢,我們還能用helper來上傳文件,上傳咱們的附件,比如多上傳幾個文件,/*** Set the given text directly as content in non-multipart mode* or as default body part in multipart mode.* Always applies the default content type "text/plain".* <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText};* else, mail readers might not be able to resolve inline references correctly.* @param text the text for the message* @throws MessagingException in case of errors*/
public void setText(String text) throws MessagingException {setText(text, false);
}這里還有一個屬性,html,默認是false的,我們把它設為true,我寫的這段內容是html,我們來重新發(fā)送
package com.learn.springboot;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.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 SpringBootTaskApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testpublic void contextLoads() {SimpleMailMessage message = new SimpleMailMessage();message.setSubject("通知-今晚開會");message.setText("今晚7:30開會");message.setTo("870116654@qq.com");message.setFrom("306758218788@qq.com");mailSender.send(message);}@Testpublic void test02() throws Exception {// 創(chuàng)建一個復雜的消息郵件MimeMessage mimeMessage = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);// 郵件設置helper.setSubject("通知-今晚開會");helper.setText("<b style='color:red'>今晚7:30開會</b>",true);helper.setTo("870116654@qq.com");helper.setFrom("306758218788@qq.com");// 上傳文件helper.addAttachment("1.jpg", new File("C:\\Users\\Leon.sun\\Desktop\\Temp\\image\\1.jpg"));helper.addAttachment("2.jpg", new File("C:\\Users\\Leon.sun\\Desktop\\Temp\\image\\2.jpg"));mailSender.send(mimeMessage);}
}
?
總結
以上是生活随笔為你收集整理的SpringBoot高级-任务-邮件任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot高级-任务-定时任务
- 下一篇: 开始使用Spring Cloud实战微服