python 利用smtplib来发送邮件,具体的代码如下
一、 编辑smtp_v2.py
vim /home/python/smtp_v2.py
#!/usr/bin/env python
# -*- encoding:utf-8 -*- '''欢迎使用python smtp''' import smtplib from email.mime.text import MIMEText
from email.utils import formataddr
from email.header import Header smtp_host = 'smtp.sina.com'
smtp_port = 25 mail_user = 'test@sina.com'
mail_pwd = '这里输入发送邮箱的登录密码' sender = 'test@sina.com'
receivers = ['test@sina.com','test@qq.com'] message = MIMEText('Python邮件发送测试...','plain','utf-8')
message['From'] = formataddr(['小灰哥',sender])
message['To'] = formataddr(['测试',receivers[0]]) subject = Header('python 测试一二三','utf-8')
message['Subject'] = subject try:
smtpObj = smtplib.SMTP(smtp_host,smtp_port)
#smtpObj.connect(mail_host,smtp_port)
smtpObj.login(mail_user,mail_pwd)
smtpObj.sendmail(sender,receivers,message.as_string())
print '邮件发送成功'
smtpObj.quit()
except smtplib.SMTPException ,e:
print 'Error:无法发送邮件',e
二、执行脚本
python smtp_v2.py