在實際開發中,可能會遇到用戶註冊之後需要郵件驗證這樣的功能,尤其是在參與到一個新項目的時候,基本都會遇到,這次分享專門抽出一節來說說在工作中郵件驗證的開發過程,並附加詳細代碼。值得注意的是,代碼是基於Java編寫的,其他語言原理大同小異。
注:如果對郵件發送還不熟悉的小夥伴可以查看《自動發送郵件實現》,裏面關於郵箱設置等信息都有詳細說明。
第一步:依賴引入
這裏由於我使用到了thymeleaf作爲模板,所以也需要使用到thymeleaf依賴;另一個就是mail依賴,具體如下:
<!-- 郵件發送依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 郵件發送模板需要使用thymeleaf依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
第二步:代碼編寫
1. 郵件實體定義
關於郵件實體定義如下:
package com.concurrency.study.mail;
import lombok.Data;
import lombok.ToString;
/**
* <p>Title: Mail</p >
* <p>Description: 郵件實體定義</p >
* <p>Company: http://www.novabeyond.com</p >
* <p>Project: concurrency</p >
*
* @author: WEIQI
* @Date: 2019-11-15 14:47
* @Version: 1.0
*/
@Data
@ToString
public class Mail {
/**
* 郵件發送方
*/
private String from;
/**
* 郵件接收方
*/
private String to;
/**
* 郵件主題
*/
private String subject;
/**
* 郵件內容
*/
private String mailContent;
}
郵件實體具體內容可根據項目需求自己定義,
2. 接口定義
這裏採用接口定義郵件發送接口,如果還有其他簡單郵件發送,在該接口自行定義即可,代碼如下:
package com.concurrency.study.mail.service;
import com.concurrency.study.mail.Mail;
import javax.validation.constraints.NotNull;
/**
* <p>Title: MailService</p >
* <p>Description: 郵件發送接口定義</p >
* <p>Company: http://www.novabeyond.com</p >
* <p>Project: concurrency</p >
*
* @author: WEIQI
* @Date: 2019-11-15 14:45
* @Version: 1.0
*/
public interface MailService {
/**
* @description: 發送激活郵件驗證
* @author: WEIQI
* @date: 2019-11-15 14:53
* @param mail 發送郵件需要的基本參數,具體參照Mail定義
* @return: int
*/
void sendActiveMail(@NotNull Mail mail);
}
3. 接口實現
郵件激活驗證代碼編寫,如下:
package com.concurrency.study.mail.service.impl;
import com.concurrency.study.mail.Mail;
import com.concurrency.study.mail.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.validation.constraints.NotNull;
/**
* <p>Title: MailServiceImpl</p >
* <p>Description: 郵件接口實現</p >
* <p>Company: http://www.novabeyond.com</p >
* <p>Project: concurrency</p >
*
* @author: WEIQI
* @Date: 2019-11-15 14:52
* @Version: 1.0
*/
@Slf4j
@Component
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender javaMailSender;
/**
* @param mail 發送郵件需要的基本參數,具體參照Mail定義
* @description: 發送激活郵件驗證
* @author: WEIQI
* @date: 2019-11-15 14:53
* @return: int
*/
@Override
public void sendActiveMail(@NotNull Mail mail) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mail.getFrom());
helper.setTo(mail.getTo());
helper.setSubject(mail.getSubject());
helper.setText(mail.getMailContent(), true);
javaMailSender.send(message);
log.info("激活驗證郵件發送成功...");
} catch (MessagingException e) {
log.error("驗證郵件發送失敗...", e);
}
}
}
具體代碼就不做解釋了,比較簡單,可以自己複製粘貼代碼運行查看效果。
4. 模板文件編寫
在resources目錄下創建templates文件夾,編寫激活郵件的HTML文件模板,如下圖所示:
具體代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>account active</title>
</head>
<body>
<p>您好, 請點擊下面鏈接激活您的賬號!</p><br/>
<a href="#" th:href="@{ http://www.baidu.com/{userId}(userId=${userId}) }">激活賬號</a>
</body>
</body>
</html>
5. 單元測試
創建單元測試類,對郵件發送方法進行測試,如下:
package com.concurrency.study.mail;
import com.concurrency.study.mail.service.MailService;
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.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.util.UUID;
/**
* <p>Title: MailTest</p >
* <p>Description: 郵件發送測試類</p >
* <p>Company: http://www.novabeyond.com</p >
* <p>Project: concurrency</p >
*
* @author: WEIQI
* @Date: 2019-11-15 15:07
* @Version: 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
@Autowired
private MailService mailService;
@Autowired
private TemplateEngine templateEngine;
@Test
public void sendActiveMailTest() {
Context context = new Context();
context.setVariable("userId", UUID.randomUUID());
String emailContent = templateEngine.process("senderTemplate", context);
Mail mail = new Mail();
mail.setFrom("[email protected]");
mail.setTo("[email protected]");
mail.setSubject("Please active account click under emial...");
mail.setMailContent(emailContent);
mailService.sendActiveMail(mail);
}
}
運行結果如下圖:
上面就是激活賬號郵件發送基本流程,具體的使用需要根據實際的業務流程對代碼做相應的調整。
想要了解實時博文,可以關注公衆號《編程之藝術》