springboot實(shí)現(xiàn)郵箱發(fā)送(激活碼)功能的示例代碼
第一步:現(xiàn)在郵箱里面開啟smtp服務(wù)

這里用163郵箱舉例,配置一下授權(quán)密碼,這個(gè)要提前記住

第二步:引入依賴
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>malisend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>malisend</name>
<description>malisend</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.malisend.MalisendApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>第三步:寫配置文件
spring:
mail:
host: smtp.163.com # 網(wǎng)站發(fā)送郵件郵箱服務(wù) host
port: 465
username: # 開啟那個(gè)服務(wù)的郵箱
password: # 開啟服務(wù)的那個(gè)認(rèn)證碼
properties:
mail:
smtp:
auth: true
socketFactory:
class: javax.net.ssl.SSLSocketFactory
starttls:
enable: true第四步:寫代碼
Order實(shí)體類:
package com.example.malisend.demos;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private int oid;
private String oname;
private float price;
}OrderManager接口
package com.example.malisend.demos;
public interface OrderManager {
void placeOrder(Order order);
}SimpleOrderManager類
package com.example.malisend.demos;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
@Service
public class SimpleOrderManager implements OrderManager {
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void placeOrder(final Order order) {
// Do the business calculations...
// Call the collaborators to persist the order...
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress("要發(fā)送給那個(gè)人的郵箱"));
mimeMessage.setFrom(new InternetAddress("開啟服務(wù)的那個(gè)郵箱"));
mimeMessage.setText("Dear " + order.getOname() + " " +
order.getPrice() + ", thanks for your order. " +
"Your order number is " + ".");
}
};
try {
this.mailSender.send(preparator);
}
catch (MailException ex) {
// simply log it and go on...
System.err.println(ex.getMessage());
}
}
}OrderController類
package com.example.malisend.demos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@Autowired
SimpleOrderManager orderManager;
@Autowired
JavaMailSenderImpl mailSender;
Order order=new Order(1,"cyl",2.8f);
@GetMapping("/sendMail")
public void sendMail(){
orderManager.setMailSender(mailSender);
orderManager.placeOrder(order);
}
}訪問:http://localhost:8080/sendMail
結(jié)果:

以上就是springboot實(shí)現(xiàn)郵箱發(fā)送(激活碼)功能的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于springboot郵箱發(fā)送激活碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Spring框架之基于Restful風(fēng)格實(shí)現(xiàn)的SpringMVC
這篇文章主要介紹了詳解Spring框架之基于Restful風(fēng)格實(shí)現(xiàn)的SpringMVC,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
SpringBoot項(xiàng)目請(qǐng)求不中斷動(dòng)態(tài)更新代碼的實(shí)現(xiàn)
在開發(fā)中,有時(shí)候不停機(jī)動(dòng)態(tài)更新代碼熱部署是一項(xiàng)至關(guān)重要的功能,它可以在請(qǐng)求不中斷的情況下下更新代碼,這種方式不僅提高了開發(fā)效率,還能加速測(cè)試和調(diào)試過程,本文將詳細(xì)介紹如何在 Spring Boot 項(xiàng)目在Linux系統(tǒng)中實(shí)現(xiàn)熱部署,特別關(guān)注優(yōu)雅關(guān)閉功能的實(shí)現(xiàn)2024-09-09
我總結(jié)的幾種@Transactional失效原因說明
這篇文章主要是我總結(jié)的幾種@Transactional失效原因說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題
這篇文章主要介紹了解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-10-10

