您好,登錄后才能下訂單哦!
spring boot中怎么發(fā)送文本郵件,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
首先要去郵箱打開POP3/SMTP權(quán)限:
然后會(huì)提供個(gè)授權(quán)碼,用來(lái)發(fā)送郵件。忘記了,可以點(diǎn)生成授權(quán)碼再次生成。
1、引入spring boot自帶的mail依賴,這里版本用的:<spring-boot.version>1.4.3.RELEASE</spring-boot.version>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>${spring-boot.version}</version> <scope>provided</scope> </dependency>
2、spring boot配置文件添加郵箱參數(shù)信息
spring.mail.host=smtp.qq.com spring.mail.username=你的qq號(hào)碼@qq.com spring.mail.password=qq郵箱開啟SMTP提供的授權(quán)碼(注意:不是你的qq郵箱密碼)#下面一般不用動(dòng) spring.mail.default-encoding=${spring.http.encoding.charset} spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=3000 spring.mail.properties.mail.smtp.writetimeout=5000 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
3、測(cè)試發(fā)送
@Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; SimpleMailMessage smm = new SimpleMailMessage(); smm.setFrom(from); smm.setTo("發(fā)送方郵件地址"); smm.setSubject("springboot測(cè)試郵件"); smm.setText("簡(jiǎn)單文本郵件測(cè)試發(fā)送!"); mailSender.send(smm);
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。