使用python发送163邮件 qq邮箱

时间:2021-04-01 08:16:59

使用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)

调用函数

send_email('hello', " 'https://www.cnblogs.com/wangjiale1 024/' ")

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)