本文主要是介绍【Python】邮件发送,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
qq 邮箱准备和设置
- 设置地址
- 如下图开启 POP3/SMTP 服务,并生成授权码。注:授权码就是登陆邮箱的密码,即为代码中的秘钥
直接上代码
#!/usr/bin/env python3
# coding=utf-8# sendEmail title content
import sys
import smtplib
from email.mime.text import MIMEText
from email.header import Headersender = 'xxx@qq.com;'
receiver = ['nnn@qq.com', 'yyy@qq.com']
ccReceiver = ['nnn@qq.com', 'yyy@qq.com']
smtpserver = 'smtp.qq.com'
#smtpserver = 'smtp.exmail.qq.com'username = 'xxx@qq.com'
password = '秘钥'def send_mail(title, content):try:msg = MIMEText(content,'plain','utf-8')if not isinstance(title,unicode):title = unicode(title, 'utf-8')msg['Subject'] = titlemsg['From'] = sendermsg['To'] = ";".join(receiver)msg['Cc'] = ";".join(ccReceiver)msg["Accept-Language"]="zh-CN"msg["Accept-Charset"]="ISO-8859-1,utf-8"smtp = smtplib.SMTP_SSL(smtpserver,465)smtp.login(username, password)smtp.sendmail(sender, receiver, msg.as_string())smtp.quit()return Trueexcept Exception, e:print str(e)return False
print sys.argv[1]
print sys.argv[2]
if send_mail(sys.argv[1], sys.argv[2]):print "done!"
else:print "failed!"
这篇关于【Python】邮件发送的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!