Fork me on GitHub

利用邮箱发短信

1、目的:采用139邮箱进行短信发送,同时开启短信提醒功能,通过邮件实现发短信的效果。

2、步骤:1、首先开启139邮箱的smtp服务协议

2、开启授权码,后续代码中替换。

3、邮件设置里设置短信提醒

 

 代码如下:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 配置SMTP服务器
smtp_server = "smtp.139.com" # 139邮箱的SMTP服务器
port = 465 # SSL加密端口
sender_email = "187**39@139.com" # 发件人邮箱
receiver_email = "187**39@139.com" # 目标手机号码对应的短信网关地址
password = "501ee**6ee00" # 139邮箱的授权码

# 创建邮件内容
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = "测试:关于优化宽带网络质量类低满原因分析流程的建议" # 邮件主题
body = '''
test content''' # 邮件正文
msg.attach(MIMEText(body, 'plain'))

try:
# 创建SMTP对象并连接到服务器
server = smtplib.SMTP_SSL(smtp_server, port)
server.login(sender_email, password) # 登录邮箱
text = msg.as_string() # 将邮件对象转换为字符串
server.sendmail(sender_email, receiver_email, text)
print("success!") # 输出成功信息
except smtplib.SMTPException as e:
print(f"发送邮件失败:{e}")
finally:
server.quit() # 退出SMTP服务器

posted @ 2025-01-22 15:05  iamorz  阅读(92)  评论(0)    收藏  举报