网易邮箱发邮件到QQ邮箱

# -*- coding: UTF-8 -*-
 
import smtplib
from email.mime.text import MIMEText
import email.mime.multipart 
from email.header import Header


# 第三方 SMTP 服务
mail_host="smtp.163.com"  #设置服务器
mail_user="[email protected]"    #用户名
mail_pass="15955031693syt"   #口令



sender = '[email protected]'
receivers = '[email protected]'  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
 
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
msg = email.mime.multipart.MIMEMultipart()
msg['From'] = "[email protected]"  # 发送者
msg['To'] =  "[email protected]"        # 接收者
 
msg['Subject'] = '第一次使用 Python SMTP 邮件'
content = '''
try:
    smtpObj = smtplib.SMTP_SSL(mail_host, 465)
    #smtpObj = smtplib.SMTP()
    #smtpObj.connect(mail_host,465)
    smtpObj.login(mail_user,mail_pass)  
    smtpObj.sendmail(sender, receivers, message.as_string())
    smtpObj.quit()
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")
'''
txt = MIMEText(content)  
msg.attach(txt)



try:
    smtpObj = smtplib.SMTP_SSL(mail_host, 465)
    #smtpObj = smtplib.SMTP()
    #smtpObj.connect(mail_host,465)
    smtpObj.login(mail_user,mail_pass)  
    smtpObj.sendmail(sender, receivers, msg.as_string())
    smtpObj.quit()
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")

猜你喜欢

转载自www.cnblogs.com/chillytao-suiyuan/p/9145062.html

相关文章