python 163邮箱发送邮件
时间: 2023-11-09 14:09:37 浏览: 112
Python可以通过smtplib和email模块来实现发送邮件。在使用163邮箱发送邮件时,需要先开启POP3/SMTP服务,并获取授权码。下面是一个示例代码:
```
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服务
mail_host = "smtp.163.com" # 设置服务器
mail_user = "xxx@163.com" # xxx是你的163邮箱用户名
mail_pass = "TTVZISPRBPGXIBIO" # 口令是你设置的163授权密码
sender = 'xxx@163.com' # 发送者邮箱
receivers = ['xxx@163.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8') # 邮箱内容
message['From'] = Header("me", 'utf-8') # 发送者显示
message['To'] = Header("me", 'utf-8') # 接受者显示
subject = 'email test' # 邮箱标题
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
```
其中,mail_host、mail_user、mail_pass需要根据自己的情况进行修改。发送邮件的内容可以通过MIMEText类来创建,邮件的标题、发送者、接收者等信息可以通过email.header.Header类来创建。在发送邮件时,需要使用smtplib.SMTP类来连接邮件服务器,并使用login方法登录自己的邮箱,最后使用sendmail方法发送邮件。
阅读全文
相关推荐













