当前位置:网站首页>发送激活邮件「建议收藏」
发送激活邮件「建议收藏」
2022-08-09 21:43:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
我们每天都会接收,发送邮件,今天就来讲一讲邮件发送的原理以及如何实现。
邮件发送的原理
基本概念
邮件服务器:跟web服务器一样,我们每一个电子邮箱的所有信息都会保存在邮件服务器上,在web服务器上也保存着各种各样的网页信息。
邮件协议:还记得我们配置Foxmail时的SMTP协议和POP3协议吗?SMTP就是发送邮件的协议。而POP是接收邮件的协议,现在常用的是第三版,所以也称POP3协议。
原理过程
假如用户[email protected]想要给[email protected]发送一封邮件,具体过程为:
[email protected]给[email protected]发送邮件
1.登录邮箱客户端
2.连接新浪的SMTP服务器
3.编写邮件并保存到本地
4.发送到163的SMTP服务器上
5.163的SMTP服务器保存邮件
6.登录邮箱客户端
7.连接到163的POP3服务器
8.找到服务器存储邮件的位置
9.收到邮件并返回到客户端
这样就实现了邮件的接收和发送。
我们在网站上注册账号的时候,一般都会收到一封激活邮件,在邮箱里点击激活链接后才能登录到网站上。所以发送激活邮件的大体思路是:
首先向用户邮箱中发送激活邮件,邮件内容为激活账号的连接,链接内容包括发送邮件的IP和激活码。
接下来就是用户激活:在邮件中点击激活链接,后台则根据传递的激活码进行用户查询,如果激活码不为空,则修改用户状态,即可以登录网站。
代码实现
首先引入jar包:mail.jar和activation.jar
发送邮件的方法
/**
* 发送邮件的方法
* @param to:收件人
* @param code:激活码
* @throws MessagingException
* @throws AddressException
*/
public static void sendMail(String to,String code) throws AddressException, MessagingException{
/**
* 1.获得一个Session对象
* 2.创建一个代表邮件的对象 Message
* 3.发送邮件 Transport
*/
//1.获得连接对象
Properties props = new Properties();
props.setProperty("mail.host", "localhost");
Session session = Session.getInstance(props, new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication("[email protected]","111");
}
});
//2.创建邮件对象
Message message = new MimeMessage(session);
try{
//设置发件人
message.setFrom(new InternetAddress("[email protected]"));
//设置收件人
message.addRecipient(RecipientType.TO, new InternetAddress(to));
// 抄送 CC 暗送 BCC
//设置邮件主题
message.setSubject("来自TGB官方商城激活邮件");
//设置邮件正文
message.setContent("<h1>TGB官方商城激活邮件!点下面连接完成激活操作</h1><h3><a href='http://192.168.21.170:8080/shop/user_active.action?code="+code+"'>http://192.168.21.170:8080/shop/user_active.action?code="+code+"</a></h3>","text/html;charset=UTF-8");
//3.发送邮件
Transport.send(message);
}catch(AddressException e){
e.printStackTrace();
}catch(MessagingException e){
e.printStackTrace();
}
}
//调用sendMail,发送邮件
public static void main(String[] args){
try {
sendMail("[email protected]","11111111");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
收到的邮件效果
用户激活
Action中用户激活的方法
<span style="font-size:18px;"> /**
* 用户激活的方法
* @return
*/
public String active(){
//根据激活码查询用户
User existUser = userService.fineByCode(user.getCode());
//判断
if(existUser == null){
//激活码错误
this.addActionMessage("激活失败:激活码错误!");
}else{
//激活成功
//修改用户的状态
existUser.setState(1);
existUser.setCode(null);
userService.update(existUser);
this.addActionMessage("激活成功,请去登录!");
}
return "msg";
}</span>
Service中的方法
<span style="font-size:18px;"> //业务层根据激活码查询用户
public User fineByCode(String code) {
return userDao.findByCode(code);
}
//修改用户状态的方法
public void update(User existUser) {
userDao.update(existUser);
}</span>
Dao中的方法
<span style="font-size:18px;"> //根据激活码查询用户
public User findByCode(String code) {
String hql = "from User where code = ?";
List<User> list = this.getHibernateTemplate().find(hql,code);
if(list != null && list.size()>0){
return list.get(0);
}
return null;
}
//修改用户状态
public void update(User existUser) {
this.getHibernateTemplate().update(existUser);
}</span>
这样我们就实现了发送激活邮件并激活的功能,理清思路再去做就OK了。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105729.html原文链接:https://javaforall.cn
边栏推荐
- 几种绘制时间线图的方法
- PMP每日一练 | 考试不迷路-8.8(包含敏捷+多选)
- Cookie、session、token
- QGIS编译SIP的问题
- In programming languages, the difference between remainder and modulo
- NIO Cup 2022 Nioke Summer Multi-School Training Camp 7 CFGJ
- 4D Summary: 38 Knowledge Points of Distributed Systems
- 抽象类 or 接口
- Deceptive Dice(期望计算)
- TF中使用zeros(),ones(), fill()方法生成数据
猜你喜欢
Wps下划线怎么弄?Wps添加下划线的最全方法
小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)
TF generates uniformly distributed tensor
2.1.5 大纲显示问题
宝塔实测-搭建LightPicture开源图床系统
6个规则去净化你的代码
Simple questions peek into mathematics
Word第一页空白页怎么删除?删除Word第一页空白页方法教程
PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
[Cloud Native] 4.2 DevOps Lectures
随机推荐
Sudoku | Backtrack-7
【stack】【queue】【priority_queue】【deque】Detailed explanation
C语言预处理命令是什么?
Word箭头上面怎么打字
Tensorflow模型整体构建流程
Wps下划线怎么弄?Wps添加下划线的最全方法
String hashing (2014 SERC J question)
PHP 二维数组根据某个字段排序
Word怎么制作一张标准的答题卡?
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
laravel table migration error [easy to understand]
别叫我玩,我要考PMP:考PMP选择机构需要了解的那些事儿
Photometric Stereo 光度立体法三维重建
Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
Several ways to draw timeline diagrams
Hessian Matrix 海森矩阵
蔚来杯2022牛客暑期多校训练营7 CFGJ
L3-2 至多删三个字符 (30 分)
hdu 1503 Advanced Fruits(最长公共子序列的应用)
TF生成均匀分布的tensor