zoukankan      html  css  js  c++  java
  • Python之邮件发送

    Python的smtplib提供了一种很方便的途径用来发送电子邮件,它有SMTP协议进行简单的封装,可以使用SMTP对象的sendmail方法发送邮件,通过help()查看SMTP所提供的方法如下:

    1 from smtplib import SMTP
    2 help(SMTP)

    导入SMTP对象,通过help()查看对象的注释,从中找到sendmail()方法的使用说明。

    connect(host.port)方法参数说明如下:

    host: 指定连接的邮箱服务器。

    port:指定连接服务器的端口号。

    login(user,password)方法参数说明如下:

    user:登录邮箱的用户名

    password: 登录邮箱的密码

    sendmail(from_addr,to_addr,msg,)方法参数说明如下:

    from_addr:邮件发送者地址。

    to_addrs:字符串列表,邮件发送地址

    msg:发送消息。

    quit()方法:用户借宿SMTP回话。

    一般发邮件时you两种方式:

    方式一:自己邮箱的weby页面(如mail.126.com),输入自己邮箱的用户名和密码登录,打开发邮件页面,填写对方的邮箱地址及邮件标题与正文,完成后点击发送。

    方式二:下载安装邮箱客户端(如:OutLook ,Foxmail等)填写邮箱账号,密码及邮箱服务器(如smtp.126.com),一般的邮箱客户端会默认记下这些信息,所这个过程只需填写一次,后面发邮件的过程与方法相同。

    通过Python的SMTP对象发邮件则更像方式二,因为需要填写邮箱服务器。在实际发送邮件时,会涉及到很多的需求,例如:邮件的正文格式,是否带图片。邮件

    *********************************

    一、发送文本/只带有正文的邮件

     1 # coding: utf-8
     2 import smtplib
     3 import time
     4 from email.mime.text import MIMEText
     5 from email.header import Header
     6 
     7 sender = 'fengyiru6369@163.com'
     8 receiver = '1194150169@qq.com'
     9 current_time = time.strftime("%Y-%m-%d-%H_%M", time.localtime(time.time()))
    10 current = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
    11 subject = 'python email test'+current
    12 smtpserver = 'smtp.163.com'
    13 username = 'fengyiru6369@163.com'
    14 password = 'FYRw19222915'  # 是授权密码,而不是登录密码
    15 
    16 #msg = MIMEText('你好,加油', 'text', 'utf-8')  # 中文需参数‘utf-8’,单字节字符不需要
    17 msg = MIMEText('你好,加油','plain','utf-8')
    18 msg['Subject'] = Header(subject, 'utf-8')
    19 msg['From'] = 'Tim<fengyiru6369@163.com>'
    20 msg['To'] = "1194150169@qq.com"
    21 smtp = smtplib.SMTP()
    22 smtp.connect('smtp.163.com')
    23 smtp.login(username, password)
    24 smtp.sendmail(sender, receiver, msg.as_string())
    25 smtp.quit()

    注:

    1.要参考http://blog.csdn.net/huochen1994/article/details/51282093 ,修改163邮箱的授权码,并且登录密码为163邮箱的授权密码

    2.注:暂时不知道msg =MIMEText()中text和plain的区别,前者执行是无法显示出正文,后者可以显示出正文。

     二、发送HTML形式的邮件

     1 # coding: utf-8
     2 import smtplib
     3 import time
     4 from email.mime.text import MIMEText
     5 from email.header import Header
     6 
     7 sender = 'fengyiru6369@163.com'
     8 receiver = '1194150169@qq.com'
     9 current_time = time.strftime("%Y-%m-%d-%H_%M", time.localtime(time.time()))
    10 current = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
    11 subject = 'python email test'+current
    12 smtpserver = 'smtp.163.com'
    13 username = 'fengyiru6369@163.com'
    14 password = 'FYR19910915'  # 是授权密码,而不是登录密码
    15 
    16 # msg = MIMEText('你好,加油', 'text', 'utf-8')  # 中文需参数‘utf-8’,单字节字符不需要
    17 # msg = MIMEText('你好,加油','plain','utf-8')
    18 msg = MIMEText('</pre><h1>你好啊,html</h1><pre>','html','utf-8')
    19 msg['Subject'] = Header(subject, 'utf-8')
       #msg['Subject'] = subject
    20 msg['From'] = 'Tim<fengyiru6369@163.com>' 
    21 msg['To'] = "1194150169@qq.com"
    22 smtp = smtplib.SMTP()
    23 smtp.connect('smtp.163.com')
    24 smtp.login(username, password)
    25 smtp.sendmail(sender, receiver, msg.as_string())
    26 smtp.quit()

    三、带图片的HTML邮件

    四、带有附件的邮件

     1 # coding: utf-8
     2 import smtplib
     3 from email.mime.multipart import MIMEMultipart
     4 from email.mime.text import MIMEText
     5 from email.mime.image import MIMEImage
     6 from email.mime.application import MIMEApplication
     7 import os.path
     8 import mimetypes
     9 import time
    10 from email.header import Header
    11 
    12 # 发送邮箱服务器
    13 smtpserver = 'smtp.163.com'
    14 # 发送邮箱
    15 sender = 'fengyiru6369@163.com'
    16 # 接收邮箱
    17 receiver = '1194150169@qq.com'
    18 # 发送邮箱用户/密码
    19 username = 'fengyiru6369@163.com'
    20 password = 'FYR19910915'  # 是授权密码,而不是登录密码
    21 
    22 current_time = time.strftime("%Y-%m-%d-%H_%M", time.localtime(time.time()))
    23 current = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    24 # 邮箱主题
    25 subject = 'python email test' + current
    26 msg = MIMEMultipart()
    27 # **两种显示邮箱主题的方法
    28 msg['Subject'] = 'test message'
    29 msg['Subject'] = Header(subject, 'utf-8')
    30 
    31 # 添加附件
    32 part = MIMEApplication(open(r'C:UsersfyrDesktop新建 Microsoft Word 文档 (2).docx', 'rb').read())
    33 part.add_header('Content-Disposition', 'attachment', filename="123.docx")
    34 msg.attach(part)
    35 
    36 # 邮件发送
    37 msg['From'] = 'Tim<fengyiru6369@163.com>'
    38 msg['To'] = "1194150169@qq.com"
    39 smtp = smtplib.SMTP()
    40 smtp.connect('smtp.163.com')
    41 smtp.login(username, password)
    42 smtp.sendmail(sender, receiver, msg.as_string())
    43 smtp.quit()

    思路是:使用MIMEMultipart来表示这个邮件是多个部分组成的,然后attach各个部分。如果是附件,则add_header加入附件的申明。

    在Python中,MIME的这些对象的继承关系如下所示:

    一般来说,不会用到MIMEBase,而是直接使用它的继承类。MIMEMultipart有attach方法,而MIMENonMultipart没有,只能被attach。

    MIME有很多中类型,如果附件时图片格式,用MIMEImage.如果是音频,用MIMEAudio,等等。

    最简便的方法,不管什么类型的附件,都用MIMEApplication,MIMEApplication默认子类型是application/octet-stream.

    application/octet-stream表明:“这是个二进制文件,希望你们那边知道怎么处理”,然后哦客户端,比如qq邮箱,收到这个申明后,会根据文件扩展名来猜测。

     1 #添加附件
     2 #---这是文字部分--- 
     3 part = MIMEText("乔装打扮,不择手段") 
     4 msg.attach(part) 
     5    
     6 #---这是附件部分--- 
     7 #xlsx类型附件 
     8 part = MIMEApplication(open('foo.xlsx','rb').read()) 
     9 part.add_header('Content-Disposition', 'attachment', filename="foo.xlsx") 
    10 msg.attach(part) 
    11    
    12 #jpg类型附件 
    13 part = MIMEApplication(open('foo.jpg','rb').read()) 
    14 part.add_header('Content-Disposition', 'attachment', filename="foo.jpg") 
    15 msg.attach(part) 
    16    
    17 #pdf类型附件 
    18 part = MIMEApplication(open('foo.pdf','rb').read()) 
    19 part.add_header('Content-Disposition', 'attachment', filename="foo.pdf") 
    20 msg.attach(part) 
    21    
    22 #mp3类型附件 
    23 part = MIMEApplication(open('foo.mp3','rb').read()) 
    24 part.add_header('Content-Disposition', 'attachment', filename="foo.mp3") 
    25 msg.attach(part) 
  • 相关阅读:
    HDU6470 ()矩阵快速幂
    O(1)乘法与快速乘O(log)
    imos 学习笔记五 抓拍 c#
    imos 学习笔记四 录像 c#
    imos 学习笔记三 下载指定时间段视频信息 c#
    imos学习笔记二 用户登录c#
    imos学习笔记一 sdk开发环境
    Hbase(nosql)体系结构有基本操作 笔记八
    zookeeper的安装与配置 笔记七
    mapReduce体系结构和各种算法 笔记六
  • 原文地址:https://www.cnblogs.com/fengyiru6369/p/7463415.html
Copyright © 2011-2022 走看看