慕课网_《Java实现邮箱验证》学习总结
時(shí)間:2017年06月07日星期三
說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com
教學(xué)示例源碼:無
個(gè)人學(xué)習(xí)源碼:https://github.com/zccodere/s...
第一章:課程介紹
1-1 課程內(nèi)容介紹
課程內(nèi)容
理解郵件開發(fā)涉及的基本概念 了解SMTP和POP3協(xié)議 掌握配置Foxmail 掌握J(rèn)avaMail發(fā)送簡單郵件第二章:郵件相關(guān)概念介紹
2-1 JavaMail的概述
JavaMail
JavaMail,顧名思義,提供給開發(fā)者處理電子郵件相關(guān)的編程接口。它是Sun發(fā)布的用來處理email的API。它可以方便地執(zhí)行一些常用的郵件傳輸。我們可以基于JavaMail開發(fā)出類似于Microsoft Outlook的應(yīng)用程序。為什么要學(xué)習(xí)JavaMail
現(xiàn)在很多的WEB開發(fā)都需要使用JavaMail,例如: 1.用戶注冊(cè)后,網(wǎng)站發(fā)送一封激活郵件驗(yàn)證。 2.用戶過生日,系統(tǒng)發(fā)送生日祝福郵件。 3.將最新活動(dòng)和優(yōu)惠以郵件的形式告知會(huì)員。 這些應(yīng)用都需要開發(fā)人員會(huì)使用編程語言發(fā)送郵件。2-2 郵件開發(fā)中的相關(guān)的術(shù)語
電子郵箱
電子郵箱(E-mail地址)需要在郵件服務(wù)器上進(jìn)行申請(qǐng),確切地說,電子郵箱其實(shí)就是用戶在郵件服務(wù)器上申請(qǐng)的一個(gè)賬戶。用戶在郵件服務(wù)器上申請(qǐng)了一個(gè)賬戶后,郵件服務(wù)器就會(huì)為這個(gè)賬號(hào)分配一定的空間,用戶從而可以使用這個(gè)賬號(hào)以及空間,發(fā)送電子郵件和保存別人發(fā)送過來的電子郵件。郵箱服務(wù)器
服務(wù)器的概念不難理解,應(yīng)為作為WEB開發(fā)人員我們應(yīng)該更清楚什么是服務(wù)器,服務(wù)器指的是一臺(tái)電腦安裝了一個(gè)服務(wù)器軟件。那么這臺(tái)電腦就可以稱為是WEB服務(wù)器。那么同樣的一臺(tái)電腦安裝了郵件服務(wù)器軟件,那么這臺(tái)電腦稱為是郵箱服務(wù)器。要在Internet上提供電子郵件功能,必須有專門的電子郵件服務(wù)器。例如現(xiàn)在網(wǎng)絡(luò)上有很多提供郵件服務(wù)的廠商:新浪、搜狐、網(wǎng)易等等他們都有自己的郵件服務(wù)器。SMTP協(xié)議(發(fā)送郵件協(xié)議)
SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協(xié)議,它是一組用于由源地址到目的地址傳送郵件的規(guī)則,由它來控制信件的中轉(zhuǎn)方式。 SMTP協(xié)議屬于TCP/IP協(xié)議簇,它幫助每臺(tái)計(jì)算機(jī)在發(fā)送或中轉(zhuǎn)信件時(shí)找到下一個(gè)目的地。 SMTP協(xié)議所指定的服務(wù)器,就可以把E-mail寄到收信人的服務(wù)器上了,整個(gè)過程只要幾分鐘。 SMTP服務(wù)器則是遵循SMTP協(xié)議的發(fā)送郵件服務(wù)器,用來發(fā)送或中轉(zhuǎn)發(fā)出的電子郵件。 通常把處理用戶SMTP請(qǐng)求(郵件發(fā)送請(qǐng)求)的郵件服務(wù)器稱為SMTP服務(wù)器。 默認(rèn)端口號(hào)為25POP3協(xié)議(接收郵件協(xié)議)
POP3,全名為“Post Office Protocol - Version 3”,即“郵局協(xié)議版本3”。 是TCP/IP協(xié)議族中的一員,由RFC1939 定義。 本協(xié)議主要用于支持使用客戶端遠(yuǎn)程管理在服務(wù)器上的電子郵件。 提供了SSL加密的POP3協(xié)議被稱為POP3S。 同樣,用戶若想從郵件服務(wù)器管理的電子郵箱中接收一封電子郵件的話,他連上郵件服務(wù)器后,也需要遵循一定的通訊格式,POP3協(xié)議用于定義這種通訊格式。 因而,通常我們也把處理用戶POP3請(qǐng)求(郵件接收請(qǐng)求)的郵件服務(wù)器稱為POP3服務(wù)器。 默認(rèn)端口號(hào)1102-3 郵件收發(fā)過程的介紹
郵件收發(fā)過程
第三章:郵箱服務(wù)器和客戶端安裝及配置
3-1 郵箱服務(wù)器的安裝和配置
安裝軟件eyoumailserversetup.exe
安裝成功后,修改域名
新建賬號(hào)
3-2 郵箱客戶端的安裝和配置
安裝軟件foxmail.exe
安裝成功后,登錄郵箱
第四章:郵件發(fā)送代碼實(shí)現(xiàn)
4-1 環(huán)境搭建
以用戶注冊(cè)為例,當(dāng)用戶注冊(cè)成功之后,向用戶注冊(cè)郵箱發(fā)送用戶激活郵件。
教學(xué)時(shí),使用傳統(tǒng)開發(fā)方式,先建數(shù)據(jù)庫表,然后在開發(fā)項(xiàng)目。我在學(xué)習(xí)時(shí),使用JPA技術(shù),基于實(shí)體對(duì)象模型進(jìn)行自動(dòng)創(chuàng)建表。
構(gòu)建工具:maven
使用框架:Spring Boot
POM文件如下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.myimooc</groupId><artifactId>myregistweb</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>myregistweb</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</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-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>1.5.3.RELEASE</version></plugin></plugins></build></project>用戶注冊(cè)頁面代碼
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"/><title>用戶注冊(cè)頁面</title> </head> <body> <form action="/regist" method="post"><table width="600" border="1"><tr><td>用戶名</td><td><input type="text" name="username"/> </td></tr><tr><td>密碼</td><td><input type="password" name="password"/> </td></tr><tr><td>昵稱</td><td><input type="text" name="nickname"/> </td></tr><tr><td>郵箱</td><td><input type="text" name="email"/> </td></tr><tr><td colspan="2"><input type="submit" value="注冊(cè)"/> </td></tr></table> </form> </body> </html>4-2 用戶注冊(cè)相關(guān)類的創(chuàng)建
代碼演示:
1.編寫領(lǐng)域模型User類
package com.myimooc.myregistweb.domain;import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;@Entity public class User {@Id@GeneratedValueprivate Integer uid;private String username;private String password;private String nickname;private String email;private Integer state;private String code;public Integer getUid() {return uid;}public void setUid(Integer uid) {this.uid = uid;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getState() {return state;}public void setState(Integer state) {this.state = state;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}}2.編寫工具類UuidUtils類
package com.myimooc.myregistweb.util;import java.util.UUID;/*** 生成隨機(jī)字符串工具類* @create ZhangCheng by 2017-06-08**/ public class UuidUtils {public static String getUuid(){return UUID.randomUUID().toString().replace("-", "");}}4-3 用戶注冊(cè)功能代碼實(shí)現(xiàn)
代碼演示:
1.編寫UserRepository類
package com.myimooc.myregistweb.dao;import org.springframework.data.jpa.repository.JpaRepository;import com.myimooc.myregistweb.domain.User;public interface UserRepository extends JpaRepository<User, Integer> {User findByCode(String code);}2.編寫UserService類
package com.myimooc.myregistweb.service;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.myimooc.myregistweb.dao.UserRepository; import com.myimooc.myregistweb.domain.User; import com.myimooc.myregistweb.util.MailUtils;@Service public class UserService {@Autowiredprivate UserRepository userRepository;/*** 用戶注冊(cè)的方法* @param user*/public void regist(User user){// 將數(shù)據(jù)存入到數(shù)據(jù)庫userRepository.save(user);// 發(fā)送一封激活郵件try {MailUtils.sendMail(user.getEmail(), user.getCode());} catch (Exception e) { // e.printStackTrace();System.out.println("郵件發(fā)送異常");}}/*** 用戶激活的方法* @param user*/public boolean registActive(String code){User user = userRepository.findByCode(code);if( null == user){return false;}user.setState(1);user.setCode("");userRepository.save(user);return true;}}3.編寫UserController類
package com.myimooc.myregistweb.web.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;import com.myimooc.myregistweb.domain.User; import com.myimooc.myregistweb.service.UserService; import com.myimooc.myregistweb.util.UuidUtils;@Controller public class RegistController {@Autowiredprivate UserService userService;/*** 跳轉(zhuǎn)到注冊(cè)頁面* @return*/@GetMapping("/regist")public ModelAndView toRegistPage(){return new ModelAndView("regist");}/*** 處理用戶注冊(cè)請(qǐng)求* @return*/@PostMapping("/regist")public ModelAndView regist(User user){user.setState(0);// 0,未激活;1,已激活String code = UuidUtils.getUuid()+UuidUtils.getUuid();user.setCode(code);// 調(diào)用業(yè)務(wù)層處理數(shù)據(jù)userService.regist(user);// 頁面跳轉(zhuǎn)return new ModelAndView("regist");}/*** 處理用戶激活請(qǐng)求* @return*/@GetMapping("/regist/active")@ResponseBodypublic Object registActive(@RequestParam("code") String code){boolean result = userService.registActive(code);return "激活狀態(tài):"+result;}}4-4 發(fā)送激活郵件
代碼演示:
package com.myimooc.myregistweb.util;import java.util.Properties;import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType;/*** 郵件發(fā)送工具類* @create ZhangCheng by 2017-06-08**/ public class MailUtils {/*** 發(fā)送郵件的方法* @param to 收件人郵箱地址* @param code 郵件的激活碼*/public static void sendMail(String to,String code) throws Exception{// 1.創(chuàng)建連接對(duì)象,連接到郵箱服務(wù)器Properties props = new Properties();Session session = Session.getInstance(props, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("service@zccoder.com", "123");}});// 2.創(chuàng)建郵件對(duì)象Message message = new MimeMessage(session);// 設(shè)置發(fā)件人message.setFrom(new InternetAddress("service@zccoder.com"));// 設(shè)置收件人message.setRecipient(RecipientType.TO, new InternetAddress(to));// 設(shè)置郵件主題message.setSubject("來自zccoder的賬號(hào)激活郵件");// 設(shè)置郵件的正文message.setContent("<h1>來自zccoder的賬號(hào)激活郵件,激活請(qǐng)點(diǎn)擊一下鏈接:</h1><h3><a href='http://localhost:8080/regist/active?code="+code+"'>http://localhost:8080/regist/active?code="+code+"</h3>", "text/html;charset=UTF-8");// 3.發(fā)送一封激活郵件Transport.send(message);}}4-5 用戶激活功能代碼實(shí)現(xiàn)
代碼演示:
package com.myimooc.myregistweb.web.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;import com.myimooc.myregistweb.domain.User; import com.myimooc.myregistweb.service.UserService; import com.myimooc.myregistweb.util.UuidUtils;@Controller public class RegistController {@Autowiredprivate UserService userService;/*** 跳轉(zhuǎn)到注冊(cè)頁面* @return*/@GetMapping("/regist")public ModelAndView toRegistPage(){return new ModelAndView("regist");}/*** 處理用戶注冊(cè)請(qǐng)求* @return*/@PostMapping("/regist")public ModelAndView regist(User user){user.setState(0);// 0,未激活;1,已激活String code = UuidUtils.getUuid()+UuidUtils.getUuid();user.setCode(code);// 調(diào)用業(yè)務(wù)層處理數(shù)據(jù)userService.regist(user);// 頁面跳轉(zhuǎn)return new ModelAndView("regist");}/*** 處理用戶激活請(qǐng)求* @return*/@GetMapping("/regist/active")@ResponseBodypublic Object registActive(@RequestParam("code") String code){boolean result = userService.registActive(code);return "激活狀態(tài):"+result;}}第五章:課程總結(jié)
5-1 課程總結(jié)
總結(jié)
JavaMial的介紹 相關(guān)術(shù)語 郵件收發(fā)過程 郵件服務(wù)器搭建和客戶端安裝 用戶注冊(cè)發(fā)送郵件激活總結(jié)
以上是生活随笔為你收集整理的慕课网_《Java实现邮箱验证》学习总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java两年工作经验面试
- 下一篇: Java制作登陆页面