`
canofy
  • 浏览: 821489 次
  • 性别: Icon_minigender_1
  • 来自: 北京、四川
社区版块
存档分类
最新评论

python发送email

阅读更多
第一种方法:
# -*- coding: utf-8 -*-

import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib

def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):

        strFrom = fromAdd
        strTo = ', '.join(toAdd)

        server = authInfo.get('server')
        user = authInfo.get('user')
        passwd = authInfo.get('password')

        if not (server and user and passwd) :
                print 'incomplete login info, exit now'
                return

        # 设定root信息
        msgRoot = MIMEMultipart('related')
        msgRoot['Subject'] = subject
        msgRoot['From'] = strFrom
        msgRoot['To'] = strTo
        msgRoot.preamble = 'This is a multi-part message in MIME format.'

        # Encapsulate the plain and HTML versions of the message body in an
        # 'alternative' part, so message agents can decide which they want to display.
        msgAlternative = MIMEMultipart('alternative')
        msgRoot.attach(msgAlternative)

        #设定纯文本信息
#        msgText = MIMEText(plainText, 'plain', 'utf-8')
#        msgAlternative.attach(msgText)

        #设定HTML信息
        msgText = MIMEText(htmlText, 'html', 'utf-8')
        msgAlternative.attach(msgText)

       #设定内置图片信息
#        fp = open('test.jpg', 'rb')
#        msgImage = MIMEImage(fp.read())
#        fp.close()
#        msgImage.add_header('Content-ID', '<image1>')
#        msgRoot.attach(msgImage)

       #发送邮件
        smtp = smtplib.SMTP()
       #设定调试级别,依情况而定
        smtp.set_debuglevel(1)
        smtp.connect(server)
        smtp.login(user, passwd)
        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
#        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
        smtp.quit()
        return

if __name__ == '__main__' :
        authInfo = {}
        authInfo['server'] = 'smtp.***.cn'
        authInfo['user'] = '***@***.cn'
        authInfo['password'] = '***'
        fromAdd = '***@***.cn'
        toAdd = ['***@163.com', '***@gamil.com']
        subject = 'title'
        plainText = '这里是普通文本'
        htmlText = '<B>HTML文本</B>'
        sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)

第二种方法:
#coding=utf-8

import smtplib,email,sys
from email.Message import Message


smtpserver='smtp.***.cn'
smtpuser='***@***.cn'
smtppass='***'
smtpport='25'

def connect():
    "connect to smtp server and return a smtplib.SMTP instance object"
    server=smtplib.SMTP(smtpserver,smtpport)
    server.ehlo()
    server.login(smtpuser,smtppass)
    return server
    
def sendmessage(server,to,subj,content):
    "using server send a email"
    msg = Message()
    msg['Mime-Version']='1.0'
    msg['From']    = smtpuser
    msg['To']      = to
    msg['Subject'] = subj
    msg['Date']    = email.Utils.formatdate()          # curr datetime, rfc2822
    msg.set_payload(content)
    try:    
        failed = server.sendmail(smtpuser,to,str(msg))   # may also raise exc
    except Exception ,ex:
        print Exception,ex
        print 'Error - send failed'
    else:
        print "send success!"

if __name__=="__main__":
    #frm=raw_input('From? ').strip()
#    to=raw_input('To? ').strip()
#    subj=raw_input('Subj? ').strip() 
    to='***@***.com'
    subj='title1111'    
    print 'Type message text, end with line="."'
    text = 'content'
#    while True:
#        line = sys.stdin.readline()
#        if line == '. ': break
#        text += line
    server=connect()
    sendmessage(server,to,subj,text)    


分享到:
评论
2 楼 lgstarzkhl 2013-09-06  
这个是windows下还是linux下的?
1 楼 lionkingzw 2013-03-10  
第一种方法 line 56 有个bug:收件人为多人时,只有第一个收件人能收到。
smtp.sendmail(strFrom, strTo, msgRoot.as_string())  

改为
smtp.sendmail(strFrom, toAdd, msgRoot.as_string())  

即可

相关推荐

    详解Python发送email的三种方式

    Python发送email的三种方式,分别为使用登录邮件服务器、使用smtp服务、调用sendmail命令来发送三种方法 Python发送email比较简单,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以...

    Python发送Email邮件案例

    使用Python编写的一个使用SMPT协议发送Email邮件的案例,适合消息监听的人群,可以实时得到消息响应

    Python发送email的3种方法

    python发送email还是比较简单的,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以使用本地或者是远程的smtp服务来发送邮件,不管是单个,群发,还是抄送都比较容易实现。 先把几个最...

    Python发送Email方法实例

    本文以实例形式展示了Python发送Email功能的实现方法,有不错的实用价值的技巧,且功能较为完善。具体实现方法如下: 主要功能代码如下: #/usr/bin/env python # -*- encoding=utf-8 -*- import base64 import ...

    python写的发送email的例子

    python写的发送email的例子,其中主要以python做为文字解析,发送email是使用了mutt. 我使用的是mutt+msmtp+getmail+procmail。

    Python-python发送邮件报表

    python发送邮件报表

    python发送qq邮箱源码

    python发送qq邮箱的代码,这里用到了Python的两个包来发送邮件: smtplib 和 email 。

    python发送邮件的脚本

    半天时间写了个python发送邮件的脚本 作者: jeffery ( email:dungeonsnd@126.com, msn:dungeonsnd@hotmail.com, csdn blog:http://blog.csdn.net/dungeonsnd) 时间: 2011-06-19 地点: SH --------------------------...

    python实现自动发送报警监控邮件

    本文实例为大家分享了python自动发送报警监控邮件 的具体代码,供大家参考,具体内容如下 因为有一些日常任务需要每日检查日否执行正确,所以需要...python send_email.py xxxxxxx@qq.com,xxxxxx@qq.com test数据 /hom

    解决python同时发送多个用户问题

    使用python发送邮件(单人/多人): 需要注意的点如下: 1、需要下载的python库有: import smtplib from email.mime.text import MIMEText from email.header import Header 2、整理一下简单的编程思路: a、首先...

    Python发送带图片邮件

    要使用Python发送带有图片的邮件,可以按照以下步骤进行: 导入所需的模块:首先,导入smtplib和email相关的模块,用于发送邮件。 创建邮件对象:使用email模块创建一个邮件对象。设置邮件的发件人、收件人、主题...

    python_email.rar

    一个封装好的使用python发送邮件的模块,可以发送html、图片、附件等,同时发送至多个收件人和抄送人。

    python写的发送email的例子-纯python版

    有两个功能: 1.发字符串到自己手机,借助139邮箱。 2.通过smtp发邮件。 个人感觉挺好用,一直在"使唤"他。 在linux和windows上正常运行。...abc 在mutt.alias中定义过了,所以,以后能用简称abc来发送地址。

    python 发送邮件

    学习中,python语言发送邮件测试代码,send email 上传

    Python实现发送email的几种常用方法

    学过Python的人都知道,实用Python实现发送email的功能还是比较简单的,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以使用本地或者是远程的smtp服务来发送邮件,不管是单个,群发,...

    python3 发送邮件包括主题和附件

    使用python3自带的库实现邮件发送,包括主题、附件等,可直接使用

    python send email via outlook

    使用python在win平台上利用outlook的com接口发送邮件, 要求, 配置python,pywin,outlook, 因为outlook有2003和2007之分, 本程序在2007下运行良好, 2003没有来得及测

    python自动发送监控数据到邮箱

    1.发送端和监控脚本都放在各个服务器上,监控脚本辅助监控服务器CPU,DISK使用情况和数据库的巡检报告。...发送邮件:配置信息都在email.ini配置文件中,包括收件箱名,附件名称。 python sentemail.py

Global site tag (gtag.js) - Google Analytics