从网上找了一些用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. HDU.1596 find the safest road (Floyd)

    HDU.1596 find the safest road (Floyd) 题意分析 与普通的最短路不太相同,本题有些许的变化. 1. 要找到由i到j最安全的路,故在求解的时候要保证mp[i][j]尽 ...

  2. 多线程中Local Store Slot(本地存储槽)[转]

    1. 使用ThreadStatic特性 ThreadStatic特性是最简单的TLS使用,且只支持静态字段,只需要在字段上标记这个特性就可以了: [ThreadStatic]   static str ...

  3. warning: React does not recognize the xxx prop on a DOM element

    这是React不能识别dom元素上的非标准attribute报出的警告,最终的渲染结果中React会移除这些非标准的attribute. 通常{...this.props}和cloneElement( ...

  4. Redux的State不应该全部放在Store里

    使用了redux管理应用的状态,应用的状态不应该全部放在Store里面. 前端状态主要有一下两种: 1. Domain data 2. UI State 1. Domain data 来自于服务端对领 ...

  5. bzoj1051: [HAOI2006]受欢迎的牛(tarjan强连通分量)

    强连通缩下点,出度为0有多个答案为0,否则答案为出度为0的强连通分量中点的个数. 发现一道tarjan模板题,顺便复习一波tarjan #include<iostream> #includ ...

  6. 51nod 1172 Partial Sums V2 卡精度的任意模数FFT

    卡精度的任意模数fft模板题……这道题随便写个表就能看出规律来(或者说考虑一下实际意义),反正拿到这题之后,很快就会发现他是任意模数fft模板题.然后我就去网上抄了一下板子……我打的是最土的任意模数f ...

  7. git使用笔记(十一)rebase

    By francis_hao    Oct 22,2017   git-rebase,改变commit的基础参照 概要 git rebase [-i | --interactive] [options ...

  8. python读书笔记-django架站过程总结(from the django book)

    django架站过程总结:1.django-admin startproject store2.store这个project的目录下有:__init__,manage,setting,urls3.se ...

  9. Azure Pipelines

    https://docs.microsoft.com/en-us/azure/devops/pipelines/?view=vsts

  10. 通过eclipse mybatis generater代码生成插件自动生成代码

    Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件.通过在Ecl ...