加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python发送各类邮件的主要方法

发布时间:2020-05-25 17:22:29 所属栏目:Python 来源:互联网
导读:python发送各类邮件的主要方法

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

msg=mail.Message.Message()    #一个实例 
msg['to']='[emailprotected]'      #发送到哪里 
msg['from']='[emailprotected]'       #自己的邮件地址 
msg['date']='2012-3-16'           #时间日期 
msg['subject']='hello world'    #邮件主题 

# -*- coding: UTF-8 -*-
'''
发送txt文本邮件
小五义:http://www.cnblogs.com/xiaowuyi
'''
import smtplib  
from email.mime.text import MIMEText  
mailto_list=[[emailprotected]] 
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 "发送失败"

# -*- coding: utf-8 -*-
'''
发送html文本邮件
小五义:http://www.cnblogs.com/xiaowuyi
'''
import smtplib  
from email.mime.text import MIMEText  
mailto_list=["[emailprotected]"] 
mail_host="smtp.XXX.com"  #设置服务器
mail_user="XXX"    #用户名
mail_pass="XXXX"   #口令 
mail_postfix="XXX.com"  #发件箱的后缀
  
def send_mail(to_list,content):  #to_list:收件人;sub:主题;content:邮件内容
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"   #这里的hello可以任意设置,收到信后,将按照设置显示
    msg = MIMEText(content,_subtype='html',_charset='gb2312')    #创建一个实例,这里设置为html格式邮件
    msg['Subject'] = sub    #设置主题
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        s = smtplib.SMTP()  
        s.connect(mail_host)  #连接smtp服务器
        s.login(mail_user,mail_pass)  #登陆服务器
        s.sendmail(me,msg.as_string())  #发送邮件
        s.close()  
        return True  
    except Exception,"<a href='http://www.cnblogs.com/xiaowuyi'>小五义</a>"):  
        print "发送成功"  
    else:  
        print "发送失败"

3、发送带附件的邮件
# -*- coding: cp936 -*-
'''
发送带附件邮件
小五义:http://www.cnblogs.com/xiaowuyi
'''

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib

#创建一个带附件的实例
msg = MIMEMultipart()

#构造附件1
att1 = MIMEText(open('d:123.rar','rb').read(),'base64','gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="123.doc"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)

#构造附件2
att2 = MIMEText(open('d:123.txt','gb2312')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="123.txt"'
msg.attach(att2)

#加邮件头
msg['to'] = '[emailprotected]'
msg['from'] = '[emailprotected]'
msg['subject'] = 'hello world'
#发送邮件
try:
    server = smtplib.SMTP()
    server.connect('smtp.XXX.com')
    server.login('XXX','XXXXX')#XXX为用户名,XXXXX为密码
    server.sendmail(msg['from'],msg['to'],msg.as_string())
    server.quit()
    print '发送成功'
except Exception,e:  
    print str(e)

# -*- coding: cp936 -*-
import smtplib
import mimetypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

def AutoSendMail():
    msg = MIMEMultipart()
    msg['From'] = "[emailprotected]"
    msg['To'] = "[emailprotected]"
    msg['Subject'] = "hello world"


    txt = MIMEText("这是中文的邮件内容哦",'plain','gb2312')     
    msg.attach(txt)
    

    file1 = "C:hello.jpg"
    image = MIMEImage(open(file1,'rb').read())
    image.add_header('Content-ID','<image1>')
    msg.attach(image)
    
    
    server = smtplib.SMTP()
    server.connect('smtp.XXX.com')
    server.login('XXX','XXXXXX')
    server.sendmail(msg['From'],msg['To'],msg.as_string())
    server.quit()
    
if __name__ == "__main__":
    AutoSendMail()

利用MIMEimage发送图片,原本是想图片能够在正文中显示,可是代码运行后发现,依然是以附件形式发送的,希望有高手能够指点一下,如何可以发送在正文中显示的图片的邮件,就是图片是附件中存在,但同时能显示在正文中

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读