吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3524|回复: 11
收起左侧

[Python 转载] Python实现发送邮件和邮件验证码

[复制链接]
倾情 发表于 2020-7-6 21:50
本帖最后由 倾情 于 2020-7-22 08:56 编辑

# 导入包
import smtplib
# 邮件内容配置
from email.mime.text import MIMEText
# 邮件头配置
from email.header import Header
# 随机数
import random
# 创建验证码变量
verify = ''
# 获取四位验证码
for i in range(1,5):
    randomstr = random.choice('qwertyuiopasdfghjklzxcvbnm1234567890')
    # print(randomstr)
    verify += randomstr
sender = 'xxxxxxx@stu.lit.edu.cn'# 发送的邮箱号
receivers = ['xxxxxx@qq.com']# 接收者邮箱
# 发送的内容、格式、编码设置
message = MIMEText(f'验证码:{verify}','html','utf-8')
# SMTP服务器为
"""
163邮箱:
163邮箱smtp服务器
pop: pop.163.com
smtp: smtp.163.com
QQ邮箱
POP3:pop.qq.com
SMTP:smtp.qq.com
SMTP端口号:25

QQ邮箱smtp服务器及端口
接收邮件服务器:imap.qq.com,使用SSL,端口号993
发送邮件服务器:smtp.qq.com,使用SSL,端口号465或587
新浪邮箱
新浪邮箱smtp服务器
外发服务器:smtp.vip.sina.com
收件服务器:pop3.vip.sina.com
新浪免费邮件
外发服务器:smtp.sina.com.cn
收件服务器:pop3.sina.com.cn
新浪免费邮箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP端口号:25
新浪VIP邮箱
POP3:pop3.vip.sina.com
SMTP:smtp.vip.sina.com
SMTP端口号:25
新浪企业邮箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP端口号:25
"""
# SMTP邮箱服务器
smtpServer = 'smtp.exmail.qq.com'
# 设置发件人
message['From'] = Header('Python 机器人','utf-8')
# 设置收件人
message['To'] = Header('185386857XX@139.com','utf-8')
# 设置主题
message['Subject'] = "邮件验证码"
try:
    # 创建一个SMTP的对象
    smtpObj = smtplib.SMTP_SSL(smtpServer,465)
    # 登录验证自己的邮箱,密码是自己生成的SMTP对应的授权码
    smtpObj.login(sender,'JxKdWpY2ZST7Uw6EU')
    # 执行发送
    smtpObj.sendmail(sender,receivers,message.as_string())
    print ("邮件发送成功")
except Exception as ex :
    print ("Error: 无法发送邮件",ex)

免费评分

参与人数 3吾爱币 +3 热心值 +3 收起 理由
Zeaf + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
peng1314 + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
RyanEdward + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Test_dada 发表于 2020-7-7 14:33
本帖最后由 Test_dada 于 2020-7-7 14:35 编辑

给你编辑下格式
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 导入包
import smtplib
# 邮件内容配置
from email.mime.text import MIMEText
# 邮件头配置
from email.header import Header
# 随机数
import random
# 创建验证码变量
verify = ''
# 获取四位验证码
for i in range(1,5):
    randomstr = random.choice('qwertyuiopasdfghjklzxcvbnm1234567890')
    # print(randomstr)
    verify += randomstr
sender = 'xxxxxxx@stu.lit.edu.cn'# 发送的邮箱号
receivers = ['xxxxxx@qq.com']# 接收者邮箱
# 发送的内容、格式、编码设置
message = MIMEText(f'验证码:{verify}','html','utf-8')
# SMTP服务器为
"""
163邮箱:
163邮箱smtp服务器
pop: pop.163.com
smtp: smtp.163.com
QQ邮箱
POP3:pop.qq.com
SMTP:smtp.qq.com
SMTP端口号:25
 
QQ邮箱smtp服务器及端口
接收邮件服务器:imap.qq.com,使用SSL,端口号993
发送邮件服务器:smtp.qq.com,使用SSL,端口号465或587
新浪邮箱
新浪邮箱smtp服务器
外发服务器:smtp.vip.sina.com
收件服务器:pop3.vip.sina.com
新浪免费邮件
外发服务器:smtp.sina.com.cn
收件服务器:pop3.sina.com.cn
新浪免费邮箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP端口号:25
新浪VIP邮箱
POP3:pop3.vip.sina.com
SMTP:smtp.vip.sina.com
SMTP端口号:25
新浪企业邮箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP端口号:25
"""
# SMTP邮箱服务器
smtpServer = 'smtp.exmail.qq.com'
# 设置发件人
message['From'] = Header('Python 机器人','utf-8')
# 设置收件人
message['To'] = Header('185386857XX@139.com','utf-8')
# 设置主题
message['Subject'] = "牵牛花微信小程序邮件验证码"
try:
    # 创建一个SMTP的对象
    smtpObj = smtplib.SMTP_SSL(smtpServer,465)
    # 登录验证自己的邮箱,密码是自己生成的SMTP对应的授权码
    smtpObj.login(sender,'JxKdWpY2ZST7Uw6EU')
    # 执行发送
    smtpObj.sendmail(sender,receivers,message.as_string())
    print ("邮件发送成功")
except Exception as ex :
    print ("Error: 无法发送邮件",ex)
魔道书生 发表于 2020-7-6 21:57
pkni1230 发表于 2020-7-6 22:46
beyond1994 发表于 2020-7-6 22:58
落下了没技术的yanlei
senlinyiye 发表于 2020-7-6 23:10
优秀,我也是这么做的
raykeyor 发表于 2020-7-7 09:29
谢谢分享
qylisten 发表于 2020-7-7 15:10
以前想做到这个,没有一个成功过。这次再试试。
小飞象v 发表于 2020-7-8 09:40
学习了。。。。。
Windfall 发表于 2020-7-21 16:41
学习了感谢大佬分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-24 01:23

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表