【python】发送邮件

从网上找了一些用python发邮件的教程,学习一下:

1、发送普通的文本邮件

http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html

http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html

抄了一份代码

# -*- coding: UTF-8 -*-
'''
发送txt文本邮件
小五义:http://www.cnblogs.com/xiaowuyi
'''
import smtplib  
from email.mime.text import MIMEText  
mailto_list=[YYY@YYY.com] 
mail_host="smtp.XXX.com"  #设置服务器
mail_user="XXXX"    #用户名
mail_pass="XXXXXX"   #口令 
mail_postfix="XXX.com"  #发件箱的后缀
  
def send_mail(to_list,sub,content):  
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"  
    msg = MIMEText(content,_subtype='plain',_charset='gb2312')  
    msg['Subject'] = sub  
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        server = smtplib.SMTP()  
        server.connect(mail_host)  
        server.login(mail_user,mail_pass)  
        server.sendmail(me, to_list, msg.as_string())  
        server.close()  
        return True  
    except Exception, e:  
        print str(e)  
        return False  
if __name__ == '__main__':  
    if send_mail(mailto_list,"hello","hello world!"):  
        print "发送成功"  
    else:  
        print "发送失败"  

问题:

1.用163邮箱发送,提示错误:(550, 'User has no permission')

2.用qq邮箱发送,提示错误:(530, 'Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28')

3.用新浪邮箱发送,提示错误:(535, '5.7.8 authentication failed')

上面种种问题显示出了用第三方邮件服务器的不便。我的解决方法是用本机的stmp服务器发送

首先要在本机上搭建一个stmp服务器。这一部分我没有做,直接大神帮忙解决了。

然后就很方便了,发送者的用户名随便写,也不用密码了。只要收件人的地址对了就可以了。

#coding=utf8

import smtplib 
import traceback 
from email.mime.text import MIMEText  
mailto_list=["xxxx@xxxx"]

def send_mail(to_list,sub,content):  
    me="me@test.com"
    msg = MIMEText(content,_subtype='plain',_charset='gb2312')  
    msg['Subject'] = sub  
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        server = smtplib.SMTP('localhost')  
        #server.connect(mail_host)  #不需要连接了
        #server.login(mail_user,mail_pass)   #不需要登录了
        server.sendmail(me, to_list, msg.as_string())  
        server.close()  
        return True  
    except Exception, e:  
        print str(e)  
        traceback.print_exc()
        return False  
if __name__ == '__main__':  
    if send_mail(mailto_list,"hello","hello world!"):  
        print "发送成功"  
    else:  
        print "发送失败"  

2、以带附件的邮件,同样是本机发送的。

具体的参数原理是什么都不清楚,就从网上copy过来了。可以用。做完了有空再看原理吧。

#coding=utf8

import smtplib 
import traceback 
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
mailto_list=["****@****"]

def send_mail(to_list,sub,content):  
    me="me@test.com"
    #创建一个带附件的实例
    msg = MIMEMultipart()
    #构造附件
    att1 = MIMEText(open("/path/somefile","rb").read(), 'base64','gb2312')
    att1["Content-Type"]='application/octet-stream'  #任意的二进制数据
    att1["Content-Disposition"] = 'attachment; filename="somename"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
    msg.attach(att1)
    msg['Subject'] = sub  
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        server = smtplib.SMTP('localhost')  
        server.sendmail(me, to_list, msg.as_string())  
        server.close()  
        return True  
    except Exception, e:  
        print str(e)  
        traceback.print_exc()
        return False  
if __name__ == '__main__':  
    if send_mail(mailto_list,"hello","hello world!"):  
        print "发送成功"  
    else:  
        print "发送失败"  
原文地址:https://www.cnblogs.com/dplearning/p/5082074.html