Last active
September 3, 2017 08:39
-
-
Save yeyuguo/2941fb64ae5b225816aefcaf82ec9301 to your computer and use it in GitHub Desktop.
python 邮件发送 SSL 方式的
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #coding:urf-8 | |
| ''' | |
| 该方式测试 没有成功 | |
| ssl 方式 | |
| ''' | |
| import email | |
| import os | |
| import email.mime.multipart | |
| from email import encoders | |
| from email.header import Header | |
| from email.mime.text import MIMEText | |
| from email.utils import parseaddr, formataddr | |
| import smtplib | |
| from_addr = '[email protected]' | |
| password = 'llheofyodztymrbhegb' # 第三方客户端需要用到的 授权码 | |
| to_addr = '[email protected]' | |
| smtp_server = 'smtp.qq.com' | |
| msg = MIMEText('hahaha','plain','utf-8') | |
| msg['From'] = 'come from QQ account' | |
| msg['To'] = '!'.join(to_addr) | |
| msg['Subject'] = Header('test email for use','utf-8').encode() | |
| server = smtplib.SMTP_SSL(smtp_server,465) | |
| server.set_debuglevel(1) | |
| server.login(from_addr,password) | |
| server.sendmail(from_addr, to_addr, msg.as_string()) | |
| server.quit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #coding:utf-8 | |
| ''' | |
| 参考文章:http://www.cnblogs.com/leetao94/p/5460520.html | |
| 该方式测试 成功发送并收到 | |
| ''' | |
| import smtplib | |
| from email.mime.text import MIMEText | |
| _user = "[email protected]" | |
| _pwd = "xxx" | |
| _to = "[email protected]" | |
| msg = MIMEText("这是邮件的内容") | |
| msg["Subject"] = "这是主题名称" | |
| msg["From"] = _user | |
| msg["To"] = _to | |
| try: | |
| s = smtplib.SMTP_SSL("smtp.qq.com", 465) | |
| s.login(_user, _pwd) | |
| s.sendmail(_user, _to, msg.as_string()) | |
| s.quit() | |
| print "Success!" | |
| except smtplib.SMTPException,e: | |
| print "Falied,%s"%e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考文章:
邮件发送 server 设置 : http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=331
qq 邮件的 授权码生成 : 设置 - 账户 的菜单栏里;