#-*-encoding:utf-8-*-
Import os, sys
Import Smtplib
From Smtplib import Smtp_ssl
From Email.header Import Header
From Email.mime.text import Mimetext
From Email.mime.multipart import Mimemultipart
From email.mime.application import mimeapplication
Mailinfo = {
#邮件发件人
"from": "******* @qq. com",
#邮件接收人列表, supports multiple Receivers.
"to": [' ******* @qq. com ', ' ******* @qq. com '],
#邮件服务器, here is the server address of QQ mailbox
"hostname": "smtp.qq.com",
#邮件发送人的用户名
"username": "* * * * * @qq. com",
#邮件发送人的密码, here is the QQ mailbox authorization code, in the ' Setup '/' account '/' Pop3/imap/smtp/exchange/carddav/caldav service '/open all services
#并记录授权码
"password": "*********",
#邮件的主题, which is the title
"mailsubject": "sliding code cracking program",
#邮件的正文内容
"mailtext": "support PYTHON3"
"images are processed using RGB comparisons only."
"subsequent increment of two value processing",
#邮件的附件
"attachment": [' slidingverification code.py '],
#邮件的编码格式, No Modification.
"mailencoding": "utf-8"
}
def SendEmail ():
#SMTP是邮件的一种协议, sending mail is subject to this Protocol
#SMTP_SSL是SMTP的加密协议, QQ mailbox is using this kind of protocol
#此处先创建一条QQ邮箱的协议
SMTP = Smtp_ssl (mailinfo["hostname"])
#这条不用管
Smtp.set_debuglevel (1)
#这条也不用管
Smtp.ehlo (mailinfo["hostname"])
#使用用户名和密码登陆QQ邮箱
Smtp.login (mailinfo["username"], Mailinfo["password"])
#MIMEText是邮件内容的载体
#此处填好邮件的正文
msg = Mimetext (mailinfo["mailtext"], "plain", mailinfo["mailencoding"])
#填好邮件的标题
msg["Subject"] = Header (mailinfo["mailsubject"], mailinfo["mailencoding"])
#填好发件人
msg["from"] = mailinfo["from"]
#填好收件人
msg["to"] = ";". Join (mailinfo["to"])
#发送邮件
Smtp.sendmail (mailinfo["from"], mailinfo["to"], msg.as_string ())
#退出
Smtp.quit ()
def sendemailwithattachment ():
#SMTP是邮件的一种协议, sending mail is subject to this Protocol
#SMTP_SSL是SMTP的加密协议, QQ mailbox is using this kind of protocol
#此处先创建一条QQ邮箱的协议
SMTP = Smtp_ssl (mailinfo["hostname"])
#这条不用管
#smtp. Set_debuglevel (1)
#这条也不用管
#smtp. EHLO (mailinfo["hostname"])
#使用用户名和密码登陆QQ邮箱
Smtp.login (mailinfo["username"], Mailinfo["password"])
#MIMEMultipart是邮件内容的载体, can carry Accessories
#此处创建该载体
msg = Mimemultipart ()
#填好邮件的正文
Msg.attach (mimetext (mailinfo["mailtext"], "plain", mailinfo["mailencoding"]))
#填好邮件的标题
msg["Subject"] = Header (mailinfo["mailsubject"], mailinfo["mailencoding"])
#填好发件人
msg["from"] = mailinfo["from"]
#填好收件人
msg["to"] = ";". Join (mailinfo["to"])
# construct Attachment 1, transfer the Test.txt file in the current directory
#循环遍历所有的附件
For Attfile in Mailinfo["attachment"]:
#读取文件内容
att = mimeapplication (open (attfile, ' RB '). read ())
#创建附件, This must be GBK format, otherwise the file name will be garbled
Att.add_header (' content-disposition ', ' attachment ', filename= (' gbk ', ', ' Attfile ')
#添加附件到邮件内
Msg.attach (att)
#发送邮件
Smtp.sendmail (mailinfo["from"], mailinfo["to"], msg.as_string ())
#退出
Smtp.quit ()
def sendemailwithhtml ():
#SMTP是邮件的一种协议, sending mail is subject to this Protocol
#SMTP_SSL是SMTP的加密协议, QQ mailbox is using this kind of protocol
#此处先创建一条QQ邮箱的协议
SMTP = Smtp_ssl (mailinfo["hostname"])
#这条不用管
Smtp.set_debuglevel (1)
#这条也不用管
Smtp.ehlo (mailinfo["hostname"])
#使用用户名和密码登陆QQ邮箱
Smtp.login (mailinfo["username"], Mailinfo["password"])
#MIMEText是邮件内容的载体
Mail_msg = "" "
<p>python Mail Send Test ...</p>
<p><a href= "http://www.runoob.com" > This is a link </a></p>
"""
#此处填好邮件的正文
msg = Mimetext (mail_msg, "html", mailinfo["mailencoding"])
#填好邮件的标题
msg["Subject"] = Header (mailinfo["mailsubject"], mailinfo["mailencoding"])
#填好发件人
msg["from"] = mailinfo["from"]
#填好收件人
msg["to"] = ";". Join (mailinfo["to"])
#发送邮件
Smtp.sendmail (mailinfo["from"], mailinfo["to"], msg.as_string ())
#退出
Smtp.quit ()
#发送邮件 (without accessories)
#sendEmail ()
#发送邮件 (with accessories)
Sendemailwithattachment ()
#发送邮件 (text is HTML)
#sendEmailWithHtml ()
Python3 Send mail via QQ server