zoukankan      html  css  js  c++  java
  • Python使用SMTP模块、email模块发送邮件

    一、smtplib模块:

    主要通过SMTP类与邮件系统进行交互。使用方法如下:

    1.实例化一个SMTP对象:

      s = smtplib.SMTP(邮件服务地址,端口号)

      s = smtplib.SMTP_SSL(邮件服务地址,端口号)

    2.登陆邮件,权限验证:

      s.login(用户名,密码)

    3.发送邮件:

      s.sendmail(发件人邮箱,收件人邮箱,发送内容)

    4.断开连接:

      s.close()

    二、email模块:

      email模块:支持发送的邮件内容为纯文本、HTML内容、图片、附件。email模块中有几大类来针对不同的邮件内容形式,常用如下:

      MIMEText:(MIME媒体类型)内容形式为纯文本、HTML页面。

      MIMEImage:内容形式为图片。

      MIMEMultupart:多形式组合,可包含文本和附件。

    每一类对应的导入方式:

      from email.mime.text import MIMEText

      from email.mime.image import MIMEImage

      from email.mime.multipart import MIMEMultipart

    三、MIMEText:

      MIMEText(msg,type,chartset)

      msg:文本内容

      type:文本类型默认为plain(纯文本)

       发送HTML格式的时候,修改为html,但同时要求msg的内容也是html的格式。

      chartset:文本编码,中文为“utf-8”

      # 构造TEXT格式的消息

      msg = MIMEText("hello.text","plain","utf-8")

      msg["Subject"] = "xxxxx"

      msg["From"] = "xxxx"

      msg["To"] = "xxxx"

      #发送以上构造的邮件内容要使用as_string将构造的邮件内容转换为string形式。

      s.sendmail("xxx","xxx",msg.as_string)

    四、MIMEImage、MIMEMultipart:

      msg = MIMEMultipart()

      #实例化一个文本对象 

      msg_sub = MIMEText("hello.text","plain","utf-8")

      #将text消息添加到MIMEMultipart中,作为邮件正文。

      msg.attach(msg_sub)

      #图片作为附件

      import os

      img_datas = open(os.getcwd()+ "/reports/xxxx.png","rb").read()

      msg_img = MIMEImage(img_data)

      msg_img.add_header('Content-Disposition','attachment', filename = "xxxx.png" )

      msg_img.add_header('Content-ID','<0>')

      #将图片添加到MIMEMultiplart中,作为附件发送。

      msg.attach(mag_img)

    源代码如下:

    发送文本邮件: 

    复制代码
     1 import smtplib
     2 from email.mime.text import MIMEText
     3 
     4 sender = 'xxxx@qq.com'   #发送人邮箱
     5 passwd = 'lkugmgywydhfff' #发送人邮箱授权码
     6 receivers = 'xxxx@qq.com' #收件人邮箱
     7 
     8 subject = 'python发邮件测试' #主题
     9 content = '这是我使用python smtplib模块和email模块自动发送的邮件'    #正文
    10 
    11 msg = MIMEText(content,'plain','utf-8')
    12 msg['Subject'] = subject
    13 msg['From'] = sender
    14 msg['TO'] = receivers
    15 
    16 try:
    17     s = smtplib.SMTP_SSL('smtp.qq.com',465)
    18     s.login(sender,passwd)
    19     s.sendmail(sender,receivers,msg.as_string())
    20     print('发送成功')
    21 
    22 except Exception:
    23     print('发送失败')
    复制代码

     发送HTML邮件: 

    复制代码
     1 import smtplib
     2 from email.mime.text import MIMEText
     3 from email.header import Header
     4 
     5 sender = 'xxxx@qq.com' #发件邮箱
     6 passwd = 'lkugmgywydhfff' #发送人邮箱授权码
     7 receivers = 'xxxx@qq.com'   #收件邮箱
     8 
     9 subject = 'python发邮Html邮件测试' #主题
    10 
    11 content = """         #内容,HTML格式
    12 <p>Python 邮件发送测试...</p>
    13 <p><a href="http://www.baidu.com">这是一个链接</a></p>
    14 """
    15 
    16 msg = MIMEText(content,'html','utf-8')
    17 # msg['Subject'] = subject
    18 msg['Subject'] = Header(subject,'utf-8')
    19 # msg['From'] = sender
    20 msg['From'] = Header('大傻子','utf-8')
    21 # msg['To'] = receivers
    22 msg['To'] = Header('二愣子','utf-8')
    23 try:
    24     s = smtplib.SMTP_SSL('smtp.qq.com',465)
    25     s.login(sender,passwd)
    26     s.sendmail(sender,receivers,msg.as_string())
    27     print('Send Success')
    28 
    29 except:
    30     print('Send Failure')
    复制代码

      

    发送图片邮件: 

    复制代码
     1 import smtplib
     2 from email.mime.image import MIMEImage
     3 from email.mime.text import MIMEText
     4 from email.mime.multipart import MIMEMultipart
     5 
     6 sender = 'xxxx@qq.com'
     7 passwd = 'lkugmgywydhfff'
     8 receivers = 'xxxx@qq.com'
     9 subject = 'python发邮带img的邮件测试' #主题
    10 
    11 # 创建一个带附件的实例
    12 msg = MIMEMultipart()
    13 msg['Subject'] = subject
    14 msg['From'] = sender
    15 msg['To'] = receivers
    16 
    17 # 创建正文
    18 msg.attach(MIMEText('使用python smtplib模块和email模块自动发送邮件测试','plain','utf-8'))
    19 
    20 # 创建图片附件
    21 import os
    22 img_file = open(os.getcwd()+"/a4.jpg",'rb').read()
    23 msg_img = MIMEImage(img_file)
    24 msg_img.add_header('Content-Disposition','attachment', filename = "a4.jpg")
    25 msg_img.add_header('Content-ID', '<0>')
    26 msg.attach(msg_img)
    27 
    28 try:
    29     s = smtplib.SMTP_SSL('smtp.qq.com',465)
    30     s.set_debuglevel(1) #输出发送邮件详细过程
    31     s.login(sender,passwd)
    32     s.sendmail(sender,receivers,msg.as_string())
    33     print('Send Succese')
    34 
    35 except:
    36     print('Send Failure')
    复制代码

      

    发送带附件的邮件:

    复制代码
     1 import smtplib
     2 from email.mime.text import MIMEText
     3 from email.mime.multipart import MIMEMultipart
     4 from email.header import Header
     5 
     6 sender = 'xxxxxx@qq.com'  #发件邮箱
     7 passwd = 'lkugmgywydhfff'  # 邮箱授权码
     8 receivers = 'xxxxxx@qq.com'  #收件邮箱
     9 
    10 subject = 'python发带附件的邮件测试' #主题
    11 # 创建一个带附件的实例
    12 msg = MIMEMultipart()
    13 msg['Subject'] = subject
    14 msg['From'] = sender
    15 msg['To'] = receivers
    16 
    17 #创建正文,将文本文件添加到MIMEMultipart中
    18 msg.attach(MIMEText('使用python smtplib模块和email模块自动发送邮件测试','plain','utf-8'))
    19 
    20 #构造附件1,传送当前目录下  文件
    21 att1 = MIMEText(open('testdata.xlsx','rb').read(),'base64','utf-8') # rb以二进制方式读取
    22 # att1["Content-Type"] = 'application/octet-stream'
    23 # filename为附件名称,可以任意写,写什么名字,邮件中显示什么名字
    24 att1["Content-Disposition"] = 'attachment; filename = "testdata.xlsx" '
    25 #将附件添加到MIMEMultipart中
    26 msg.attach(att1)
    27 
    28 #构造附件2
    29 att2 = MIMEText(open('db.cfg','rb').read(),'base64','utf-8')
    30 # att2["Content-Type"] = 'application/octet-stream'
    31 att2["Content-Disposition"] = 'attachment; filename = "db.cfg" '
    32 #将附件添加到MIMEMultipart中
    33 msg.attach(att2)
    34 
    35 try:
    36     s = smtplib.SMTP_SSL('smtp.qq.com',465)
    37     s.set_debuglevel(1) #输出发送邮件详细过程
    38     s.login(sender,passwd)
    39     s.sendmail(sender,receivers,msg.as_string())
    40     print('Send Succese')
    41 
    42 except:
    43     print('Send Failure')
    复制代码
     
     
     
    转自:https://www.cnblogs.com/lizhe860/p/9079234.html
  • 相关阅读:
    第二周作业
    20172331 《Java程序设计》第1周学习总结
    20172316《程序设计与数据结构》(下)课程总结
    哈夫曼编码测试博客
    20172316 2017-2018-2 《程序设计与数据结构》实验三报告
    20172316 2018-2019-1《程序设计与数据结构》第九周学习总结
    20172316 2018-2019-1 《程序设计与数据结构》实验二报告
    2018-2019-1 20172316《程序设计与数据结构》第八周学习总结
    20172316 2018-2019-1《程序设计与数据结构》第七周学习总结
    20172316 《程序设计与数据结构》第六周学习总结
  • 原文地址:https://www.cnblogs.com/liuyanhang/p/10895683.html
Copyright © 2011-2022 走看看