python 发送邮件 163_使用python发送163邮件 qq邮箱

使用python发送163邮件

def send_email(title, content):

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

mail_host = 'smtp.163.com'

mail_user = 'wangjialexxxxxxxx@163.com'

# 这个是授权码 不是密码 需要去163设置

mail_pass = 'wangjialexxxxx'

sender = 'wangjialexxxxxxx@163.com'

receivers = ['wangjialexxxx@163.com']

#构造message (邮件内容)

message = MIMEText(content, 'plain', 'utf-8')

message['Subject'] = title

message['From'] = sender

message['To'] = receivers[0]

# smtp = smtplib.SMTP(mail_host, 587)

try:

#smtp = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465

smtp = smtplib.SMTP()

smtp.connect(mail_host)

smtp.set_debuglevel(1)

smtp.ehlo()

smtp.starttls()

smtp.ehlo()

smtp.login(mail_user, mail_pass)

smtp.sendmail(sender, receivers, message.as_string())

smtp.quit()

print("mail has been send successfully.")

except smtplib.SMTPException as e:

print(e)

调用函数

qq邮箱

import smtplib

from email import encoders

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email.mime.multipart import MIMEMultipart

#sender发件人,password服务器授权码, mail_host 服务器地址(QQsmtp) receiver接收者

sender = 'xxxxxxx@qq.com'

password = '####'

mail_host = 'smtp.qq.com'

receives = [ 'xxxxxxx@xx.com','xxxxxxx1@xx.com','xxxxxxx2@xx.com',]

#设置邮件信息

msg = MIMEMultipart()

#邮件主题

msg['Subject'] = input("请输入邮件主题: ")

msg['From'] = sender

msg_content = input("请输入正文:")

msg.attach(MIMEText(msg_content,'plain','utf-8'))

#登录并发送

try:

s = smtplib.SMTP_SSL("smtp.qq.com", 465)

s.set_debuglevel(1)

s.login(sender, password)

#给接收者发送消息

for i in range(len(receives)):

to = receives[i]

msg['To'] = to

s.sendmail(sender, to, msg.as_string())

print('success!')

s.quit()

print('All email has been send over')

except smtplib.SMTPException as e:

print("Failed ,%s",e)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值