#!/usr/bin/python3
import smtplib
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import parseaddr, formataddr
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
from_addr = "mymail@domain.com"
password = "clear-text_passwd"
to_addr = "tomail@domain.com"
smtp_server = "smtp.exmail.qq.com"
msg = MIMEMultipart('alternative')
msg['From'] = _format_addr('myname <%s>' % from_addr)
msg['To'] = _format_addr('toname <%s>' % to_addr)
msg['Subject'] = Header('一封来自银河系外的测试邮件,请倒立后开启', 'utf-8').encode()
msg.attach(MIMEText("Hi, It's a AutoSend EMail Script test.\n\nThe End", 'plain', 'utf-8'))
msg.attach(MIMEText("""
<html>
<body>
<h1>Hi, It's a AutoSend EMail Script test.</h1>
<p align="center"><img src="http://www.baidu.com/img/bd_logo1.png"></p>
<p>The End<p>
</body>
</html>
""",'html', 'utf-8'))
server = smtplib.SMTP_SSL(smtp_server, 465) # 注意,非SSL使用smtp.SMTP()
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
测试的时候,写了个循环给自己发送100封邮件,没做条件,结果就触发了发信频率限制,直接就拒绝请求550,问了客服,才知道分钟小时日都有发信频率限制。
博客:http://zzir.cn
欢迎留言关注
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。