从网上找了一些用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 "发送失败"

【python】发送邮件的更多相关文章

  1. python发送邮件

    python发送邮件(无附件) ======================================================= #!/usr/bin/env python#coding ...

  2. python发送邮件及附件

    今天给大伙说说python发送邮件,官方的多余的话自己去百度好了,还有一大堆文档说实话不到万不得已的时候一般人都不会去看,回归主题: 本人是mac如果没有按照依赖模块的请按照下面的截图安装 导入模块如 ...

  3. python 发送邮件实例

    留言板回复作者邮件提醒 -----------2016-5-11 15:03:58-- source:python发送邮件实例

  4. 解读Python发送邮件

    解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...

  5. python 发送邮件例子

    想到用python发送邮件 主要是服务器 有时候会产生coredump文件  ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生cored ...

  6. 利用python发送邮件

    找了很多使用python发送邮件的文章, 发现写的并不是太全, 导致坑特别多, 刚把这个坑跨过去, 在此记录下来 本代码使用163作为发送客户端, 接收邮箱随意 首先登录163邮箱, 开启POP3/S ...

  7. 用Python发送邮件

    文件:send.py # -*- coding:utf-8 -*- # ## 任兴测试用Python发送邮件 import os import sys import getopt import tim ...

  8. ETL过程跑完后,使用python发送邮件

    目标库中,如果有行数为0的表,使用python发送邮件 # -*- coding:utf-8 -*- # Author: zjc # Description:send monitor info to ...

  9. 【转】【Python】Python发送邮件(常见四种邮件内容)

    在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调 ...

  10. 使用python发送邮件

    最近需要做一个功能,统计bug的数量,然后发邮件给指定人,所有就先要了解一下使用python发送邮件 代码如下: #coding: utf-8 import smtplib from email.MI ...

随机推荐

  1. BZOJ5329: [SDOI2018]战略游戏——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5329 https://www.luogu.org/problemnew/show/P4606 省选 ...

  2. BZOJ1012:[JSOI2008]最大数——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1012 https://www.luogu.org/problemnew/show/P1198 现在 ...

  3. CF17E:Palisection——题解

    https://vjudge.net/problem/CodeForces-17E http://codeforces.com/problemset/problem/17/E 题目大意:给一个长度为n ...

  4. Codeforces Round #340 (Div. 2) A

    A. Elephant time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  5. JavaScript实现35选7并记录历史状态

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAL4AAABQCAYAAACnOs9vAAAJy0lEQVR4nO2dbWwUxxnH/2c5SElQSl ...

  6. executeQuery与executeUpdate

    executeQuery就是查   executeUpdate是更新,那就是增删改,和新建     吗?

  7. hdu4085 Peach Blossom Spring

    Peach Blossom Spring http://acm.hdu.edu.cn/showproblem.php?pid=4085 Time Limit: 10000/5000 MS (Java/ ...

  8. 2015/8/29 Python基础(3):数值

    数字提供了标量储存和直接访问,是不可更改类型,每次变更数值会产生新的对象.Python支持多种数字类型,包括整型.长整型.布尔型.双精度浮点.十进制浮点和复数.在Python中,变量并不是一个盒子,而 ...

  9. Java包名命名规则

    1.  sun公司建议java包的命名规则为域名的倒写. 比如:sun公司www.sun.com 就用该是com.sun.www 2. indi : 个体项目,指个人发起,但非自己独自完成的项目,可公 ...

  10. vs 统计有效代码行数

    1.Visual Studio中,crtl+Shift+F,输入b*[^:b#/]+.*$ ,查找范围:选择整个解决方案,查找选项:使用正则表达式,文件类型:*.cs;*.cshtml  选择查找全部