# -*- coding:cp936 -*-
#!/usr/bin/env python
import sys,smtplib
"""
使用smtp.sina.com作为邮件发送服务器
"""
server = "smtp.sina.com"
fromaddr= "[email protected]"
toaddr = "[email protected]"
msg = """
to:%s
from:%s
Hello,I am smtp server
""" %(toaddr,fromaddr)
s = smtplib.SMTP(server)
# 进行认证,通过后可以发送邮件
s.login("jcodeer","邮箱密码")
"""
ehlo返回值:
code:来自服务器的相应码。
string:这个响应码所对应的字符串描述。
"""
code = s.ehlo()[0]
usesesmtp = 1
if not (200 <= code <= 299):
usesesmtp = 0
"""
code和string与ehlo含义相同。
"""
code = s.helo()[0]
if not (200 <= code <= 299):
raise SMTPHeloError(code,resp)
if usesesmtp and s.has_extn("size"):
print "Maximum message size is",s.esmtp_features["size"]
if len(msg) > int(s.esmtp_features["size"]):
print "Message too large;aborting."
sys.exit(2)
s.sendmail(fromaddr,toaddr,msg)
|