前言:2014-05-22记录在hi baidu上,现在移过来
使用python向gmail发邮件
代码语言:javascript
复制
"""
发送邮件
1: 需要提供发送者的邮件、密码;接收者地址;
2:步骤:
a:Login 失败说明无法登陆;
b:登录成功则可以发送邮件;
c:退出
"""
class SendMailToServer():
"""docstring for SendMailToServer"""
def __init__(self, fromAddress, password, toAddress):
# my test mail
self.mail_username = fromAddress
self.mail_password = password
self.from_addr = self.mail_username
self.to_addrs = toAddress
# HOST & PORT
self.HOST = 'smtp.gmail.com'
self.PORT = 25
# Create SMTP Object
self.smtp = smtplib.SMTP()
def Login(self):
# 打印调试信息
self.smtp.set_debuglevel(1)
try:
self.smtp.connect( self.HOST, self.PORT )
except Exception, e:
print e, 'connect failed ...'
return False
# gmail uses ssl
self.smtp.starttls()
# login with username & password
try:
print 'loginning ...'
self.smtp.login( self.mail_username, self.mail_password )
except Exception, e:
print e, 'login faild ...'
return False
return True</code></pre></div></div>