邮件二次验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import smtplib from email.mime.text import MIMEText # 第三方 SMTP 服务 mail_host = "smtp.sina.cn" # SMTP服务器 #网易是 smtp.163.com #腾讯是 smtp.qq.com mail_pass = "admin123456" # 授权密码,非登录密码 #新浪是登陆密码 #163和QQ是授权密码 title = 'Python原生方法群发邮件发送测试' # 邮件主题 content = '此账号仅供测试,请大神们不要修改邮箱密码。' #内容 def sendEmail(): message = MIMEText(content, 'plain' , 'utf-8' ) # 内容, 格式, 编码 message[ 'From' ] = "{}" . format (sender) # # 发件人邮箱(最好写全, 不然会失败) message[ 'To' ] = "," .join(receivers) # # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 message[ 'Subject' ] = title # 邮件主题 try : smtpObj = smtplib.SMTP_SSL(mail_host, 465 ) # 启用SSL发信, 端口一般是465 smtpObj.login(mail_user, mail_pass) # 登录验证 smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 print ( "邮件发送成功!注意查收!!!垃圾箱!!!反垃圾拦截!!" ) except smtplib.SMTPException as e: print (e) #错误信息 sendEmail() #调用实例化 进行发送邮件 # if __name__ == '__main__': # sendEmail() #调用实例化 进行发送邮件 群发邮件功能 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import smtplib from email.header import Header from email.mime.text import MIMEText # 第三方 SMTP 服务 mail_host = "smtp.sina.cn" # SMTP服务器 mail_pass = "admin123456" # 授权密码,非登录密码 title = 'Python原生Header方法邮件发送测试' # 邮件主题 content = '此账号仅供测试,请大神们不要修改邮箱密码。' #内容 # SMTP服务器 # 登陆 #密码 #发送给谁 #主题 #内容 def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content): email_client = smtplib.SMTP(SMTP_host) # SMTP服务器 email_client.login(from_account, from_passwd) # SMTP服务器 登陆 #密码 # create msg msg = MIMEText(content, 'plain' , 'utf-8' ) msg[ 'Subject' ] = Header(subject, 'utf-8' ) # subject #邮件头(主题 )#一定要用Header格式化 msg[ 'From' ] = from_account #内容 字符串 msg[ 'To' ] = to_account #发送给谁 字符串 email_client.sendmail(from_account, to_account, msg.as_string()) #发送模式 print ( "邮件发送成功!注意查收!!!垃圾箱!!!反垃圾拦截!!" ) email_client.quit() #退出 # SMTP服务器 # 用户名 # 密码 # 接收 # 主题 #内容 send_email2(mail_host, mail_user, mail_pass, receiver, title, content) #调用实例化 # if __name__ == '__main__': # # SMTP服务器 # 用户名 # 密码 # 接收 # 主题 #内容 # send_email2(mail_host, mail_user, mail_pass, receiver, title, content) #调用实例化 单发邮件功能 |
注意被拦截的邮件
例
#-*- coding: utf-8 -*-
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.header import Header
# 发送邮件
def sendEmail(countent, name):
"""
发送邮件
:param countent: 发送的内容
:return:
"""
mail_text = 'aaaa \n\n'
mail_text = mail_text + '错误输出: ' + str(countent)
mail_info = {
"from": "13.com", #自己邮箱
"to": "1.com", #发给谁
"hostname": "smtp.163.com", #163或者qq
"username": "111.com", #自己邮箱
"password": "aaa", # 授权码
"mail_subject": '【爬虫脚本{0}.py】:'.format(name),
"mail_text": mail_text,
"mail_encoding": "utf-8"
}
# 这里使用SMTP_SSL就是默认使用465端口
smtp = SMTP_SSL(mail_info["hostname"])
smtp.set_debuglevel(1)
smtp.ehlo(mail_info["hostname"])
smtp.login(mail_info["username"], mail_info["password"])
msg = MIMEText(mail_info["mail_text"], "plain", mail_info["mail_encoding"])
msg["Subject"] = Header(mail_info["mail_subject"], mail_info["mail_encoding"])
msg["from"] = mail_info["from"]
msg["to"] = mail_info["to"]
smtp.sendmail(mail_info["from"], mail_info["to"].split(','), msg.as_string())
smtp.quit()
sendEmail('123wqe',111)