python使用163邮箱发送邮件

开启163的smtp服务器(绑定自己的手机)

163邮箱地址:https://mail.163.com/

记住自己的SMTP服务器

 

 Smtp服务器地址 和 端口

Ssl: smtp.163.com465

非ssl : smtp.163.com25

#coding:utf -8

import smtplib #smtp服务器
from email.mime.text import MIMEText #邮件文本

#邮件构建

subject = "滴滴答答"#邮件标题
sender = "*********@163.com"#发送方
content = "新年快乐!"
recver = "*******@qq.com"#接收方
password = "*****"邮箱密码
message = MIMEText(content,"plain","utf-8")
#content 发送内容     "plain"文本格式   utf-8 编码格式

message['Subject'] = subject #邮件标题
message['To'] = recver #收件人
message['From'] = sender #发件人

smtp = smtplib.SMTP_SSL("smtp.163.com",994) #实例化smtp服务器
smtp.login(sender,password)#发件人登录
smtp.sendmail(sender,[recver],message.as_string()) #as_string 对 message 的消息进行了封装
smtp.close()

 

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
使用Python通过163邮箱发送邮件,你需要使用SMTP协议。下面是一份示例代码,可以实现这个功能: ```python import smtplib from email.mime.text import MIMEText from email.header import Header # 发件人邮箱地址 sender = 'your_email@163.com' # 发件人邮箱密码或者授权码 password = 'your_password' # 收件人邮箱地址 receivers = ['recipient_email@domain.com'] # 邮件主题 subject = 'Python SMTP 邮件测试' # 邮件正文内容 content = '这是一封通过 Python 发送的邮件' # 设置邮件正文为纯文本格式 message = MIMEText(content, 'plain', 'utf-8') message['From'] = Header('Python邮件测试', 'utf-8') # 发件人名称 message['To'] = Header('测试', 'utf-8') # 收件人名称 message['Subject'] = Header(subject, 'utf-8') # 邮件主题 try: smtpObj = smtplib.SMTP_SSL('smtp.163.com', 465) smtpObj.login(sender, password) smtpObj.sendmail(sender, receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException as e: print("Error: 无法发送邮件", e) ``` 需要注意的是,在发送邮件之前,你需要将 `your_email@163.com` 替换成你自己的163邮箱地址,并将 `your_password` 替换成你的邮箱密码或者授权码。同时,将 `recipient_email@domain.com` 替换成收件人的邮箱地址。 这个示例代码中,我们使用了SMTP_SSL来加密连接。如果你使用的是Python 2.x版本,你需要将 `email.mime.text` 替换成 `email.MIMEText`,并将 `smtpObj = smtplib.SMTP_SSL('smtp.163.com', 465)` 替换成 `smtpObj = smtplib.SMTP_SSL('smtp.163.com', 465, timeout=120)`,其中 `timeout` 是连接超时时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值