摘要:時(shí)間年月日星期三說明本文部分內(nèi)容均來自慕課網(wǎng)。用戶過生日,系統(tǒng)發(fā)送生日祝福郵件。將最新活動(dòng)和優(yōu)惠以郵件的形式告知會(huì)員。通常把處理用戶請(qǐng)求郵件發(fā)送請(qǐng)求的郵件服務(wù)器稱為服務(wù)器。提供了加密的協(xié)議被稱為。
時(shí)間:2017年06月07日星期三
說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com
教學(xué)示例源碼:無
個(gè)人學(xué)習(xí)源碼:https://github.com/zccodere/s...
課程內(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)為25
POP3協(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文件如下
4.0.0 com.myimooc myregistweb 0.0.1-SNAPSHOT jar myregistweb Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-mail org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin 1.5.3.RELEASE
用戶注冊(cè)頁面代碼
4-2 用戶注冊(cè)相關(guān)類的創(chuàng)建用戶注冊(cè)頁面
代碼演示:
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 @GeneratedValue private 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 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 { @Autowired private 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 { @Autowired private 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") @ResponseBody public 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() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]", "123"); } }); // 2.創(chuàng)建郵件對(duì)象 Message message = new MimeMessage(session); // 設(shè)置發(fā)件人 message.setFrom(new InternetAddress("[email protected]")); // 設(shè)置收件人 message.setRecipient(RecipientType.TO, new InternetAddress(to)); // 設(shè)置郵件主題 message.setSubject("來自zccoder的賬號(hào)激活郵件"); // 設(shè)置郵件的正文 message.setContent("4-5 用戶激活功能代碼實(shí)現(xiàn)來自zccoder的賬號(hào)激活郵件,激活請(qǐng)點(diǎn)擊一下鏈接:
http://localhost:8080/regist/active?code="+code+"
", "text/html;charset=UTF-8"); // 3.發(fā)送一封激活郵件 Transport.send(message); } }
代碼演示:
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 { @Autowired private 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") @ResponseBody public 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ā)送郵件激活
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70144.html
時(shí)間:2017年07月09日星期日說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:概述 1-1 課程概述 主要內(nèi)容 驗(yàn)證碼歷史 課程內(nèi)容 不同方案對(duì)比 設(shè)計(jì)與實(shí)現(xiàn) 總結(jié) 1-2 驗(yàn)證碼歷史 驗(yàn)證碼歷史 無驗(yàn)證碼:垃圾騷擾 Luis von Ahn:Captcha 不斷...
摘要:時(shí)間年月日星期一說明本文部分內(nèi)容均來自慕課網(wǎng)。多用于網(wǎng)絡(luò)加密。散列函數(shù)函數(shù)或消息摘要函數(shù)主要作用散列函數(shù)用來驗(yàn)證數(shù)據(jù)的完整性。 時(shí)間:2017年4月10日星期一說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)示例源碼:https://github.com/zccodere/s...個(gè)人學(xué)習(xí)源碼:https://github.com/zccodere...
摘要:慕課網(wǎng)發(fā)送郵件學(xué)習(xí)總結(jié)時(shí)間年月日星期六說明本文部分內(nèi)容均來自慕課網(wǎng)。 慕課網(wǎng)《Spring Boot 發(fā)送郵件》學(xué)習(xí)總結(jié) 時(shí)間:2018年09月08日星期六 說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):https://www.imooc.com 教學(xué)源碼:https://github.com/ityouknow/... 學(xué)習(xí)源碼:https://github.com/zccoder...
時(shí)間:2017年4月13日星期四說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)示例源碼:https://github.com/zccodere/s...個(gè)人學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:概述 1-1 數(shù)字簽名算法概述 數(shù)字簽名算法 簽名-安全性,抗否認(rèn)性 數(shù)字簽名:帶有密鑰(公鑰、私鑰)的消息摘要算法 驗(yàn)...
摘要:時(shí)間年月日星期五說明本文部分內(nèi)容均來自慕課網(wǎng)。本套課程介紹微信公眾號(hào)開發(fā),主要涉及公眾號(hào)介紹編輯模式介紹開發(fā)模式介紹等。慕課網(wǎng)是垂直的互聯(lián)網(wǎng)技能免費(fèi)學(xué)習(xí)網(wǎng)站。 時(shí)間:2017年08月11日星期五說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:https://github.com/zccodere/s...學(xué)習(xí)源碼:https://github...
閱讀 2983·2023-04-25 22:16
閱讀 2186·2021-10-11 11:11
閱讀 3272·2019-08-29 13:26
閱讀 616·2019-08-29 12:32
閱讀 3438·2019-08-26 11:49
閱讀 3027·2019-08-26 10:30
閱讀 1975·2019-08-23 17:59
閱讀 1545·2019-08-23 17:57