1. #coding=utf-8
  2. #Python 3.4 https://docs.python.org/3.4/library/
  3. #IDE:Visual Studio 2015 Window10
  4. import atexit
  5. import os
  6. import unicodedata
  7. import sys
  8. import time
  9. import unicodedata
  10. import winsound
  11. import code
  12. import codecs
  13. import math
  14. import csv
  15. import base64
  16. import re
  17. import smtplib
  18. from email.mime.text import MIMEText
  19. from email.mime.multipart import MIMEMultipart
  20. from email.header import Header
  21. from email.mime.image import MIMEImage
  22. from email.mime.base import MIMEBase
  23. from email.utils import COMMASPACE, formatdate
  24. import email
  25. import os.path
  26. import sys
  27. import mimetypes
  28. import configparser
  29. import string
  30.  
  31. #https://docs.python.org/3.4/library/email.html
  32. #https://docs.python.org/3/library/email-examples.html
  33.  
  34. inifile='F:/Python/055.JPG'
  35. #config=ConfigParser.ConfigParser()
  36. #config.read(inifile)
  37. #os.remove(inifile) #移除文件
  38. subject=Header("缔友计算机有限公司销售报告","utf-8") #邮件标题
  39. ReplyToName="geovindu@dusystem.com"
  40. ReplyToMail="geovindu@dusystem.com"
  41. To="463588883@qq.com"
  42. bcc="geovindu@163.com";
  43. cc="geovindu@jw28.com";
  44. COMMASPACE = ', ';
  45. file_name=inifile #"file_name"
  46. From = "%s<geovindu@dusystem.com>" % Header("缔友计算机信息有公司涂聚文","utf-8") #发件人和姓名
  47. server = smtplib.SMTP("mail.dusystem.com",25)
  48. server.login("geovindu@dusystem.com","888") #仅smtp服务器需要验证时
  49.  
  50. # 构造MIMEMultipart对象做为根容器
  51. main_msg = MIMEMultipart("alternative"); #alternative related 2.x版本的email.MIMEMultipart.MIMEMultipart()
  52. # 构造MIMEText对象做为邮件显示内容并附加到根容器
  53. text_msg =MIMEText("xxx帮你转发的邮件", _subtype='html',_charset="utf-8") #邮件内容 2.x版本的 email.MIMEText.MIMEText
  54. main_msg.attach(text_msg)
  55.  
  56. # 构造MIMEBase对象做为文件附件内容并附加到根容器
  57.  
  58. #图片未显示,附件不同
  59. #fp = open(file_name, 'rb')
  60. #msgImage = MIMEImage(fp.read())
  61. #fp.close()
  62. #msgImage.add_header('Content-ID', '<image1>')
  63. #main_msg.attach(msgImage)
  64.  
  65. ## 设置附件头
  66. #basename = os.path.basename(file_name)
  67. #file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
  68. file_msg = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8') #发文件
  69. file_msg["Content-Type"] = 'application/octet-stream'
  70. file_msg["Content-Disposition"] = 'attachment; filename="055.JPG"'
  71.  
  72. main_msg.attach(file_msg)
  73. # 设置根容器属性
  74. main_msg['From'] = From
  75. if ReplyToMail!='none':
  76. main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail)
  77. main_msg['To'] = To;
  78. main_msg['Subject'] = subject;
  79. main_msg['Cc']=cc;
  80. main_msg['Bcc'] = bcc; #这无效
  81. main_msg['Date'] =formatdate(localtime=True)
  82. # 得到格式化后的完整文本
  83. fullText = main_msg.as_string()
  84. # 用smtp发送邮件
  85. try:
  86. server.sendmail(From, To.split(';'), fullText)
  87. finally:
  88. server.quit()
  89. #os.remove(file_name)
  90. print("发送邮件成功");

  上发图片

下为发文件

  1. #coding=utf-8
  2. #Python 3.4 https://docs.python.org/3.4/library/
  3. #IDE:Visual Studio 2015 Window10
  4. import atexit #类库
  5. import os
  6. import unicodedata
  7. import sys
  8. import time
  9. import unicodedata
  10. import winsound
  11. import code
  12. import codecs
  13. import math
  14. import csv
  15. import base64
  16. import re
  17. import smtplib
  18. from email.mime.text import MIMEText
  19. from email.mime.multipart import MIMEMultipart
  20. from email.header import Header
  21. from email.mime.image import MIMEImage
  22. from email.mime.base import MIMEBase
  23. from email.encoders import encode_base64
  24. from email.mime.application import MIMEApplication
  25. from email.utils import COMMASPACE, formatdate
  26. import email
  27. import os.path
  28. import sys
  29. import mimetypes
  30. import configparser
  31. import string
  32. import mimetypes
  33. import base64;
  34.  
  35. #https://docs.python.org/3.4/library/email.html
  36. #https://docs.python.org/3/library/email-examples.html
  37.  
  38. inifile=u'F:/Python/工应用文写作.doc'
  39. #config=ConfigParser.ConfigParser()
  40. #config.read(inifile)
  41. #os.remove(inifile) #移除文件
  42. subject=Header("缔友计算机有限公司销售报告","utf-8") #邮件标题
  43. ReplyToName="geovindu@dusystem.com"
  44. ReplyToMail="geovindu@dusystem.com"
  45. To="463588883@qq.com"
  46. bcc="geovindu@163.com";
  47. cc="geovindu@jw28.com";
  48. COMMASPACE = ', ';
  49. file_name=inifile #"file_name"
  50. From = "%s<geovindu@dusystem.com>" % Header("缔友计算机信息有公司涂聚文","utf-8") #发件人和姓名
  51. server = smtplib.SMTP("mail.dusystem.com",25)
  52. server.login("geovindu@dusystem.com","888") #仅smtp服务器需要验证时
  53.  
  54. # 构造MIMEMultipart对象做为根容器
  55. main_msg = MIMEMultipart("alternative"); #alternative related 2.x版本的email.MIMEMultipart.MIMEMultipart()
  56. # 构造MIMEText对象做为邮件显示内容并附加到根容器
  57. text_msg =MIMEText("请你及阅完文档,提出建议", _subtype='html',_charset="utf-8") #邮件内容 2.x版本的 email.MIMEText.MIMEText
  58. main_msg.attach(text_msg)
  59.  
  60. # 构造MIMEBase对象做为文件附件内容并附加到根容器
  61.  
  62. #图片未显示,附件不同
  63. #fp = open(file_name, 'rb')
  64. #msgImage = MIMEImage(fp.read())
  65. #fp.close()
  66. #msgImage.add_header('Content-ID', '<image1>')
  67. #main_msg.attach(msgImage)
  68.  
  69. ## 设置附件头
  70. #basename = os.path.basename(file_name)
  71. #file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
  72. #图片
  73. #file_image="F:/Python/055.jpg";
  74. #file_msg = MIMEText(open(file_image, 'rb').read(), 'base64', 'utf-8') #发完文件不见了
  75. #file_msg["Content-Type"] = 'application/octet-stream' # 图片(application/octet-stream)可以,但WORD不行
  76. #file_msg["Content-Disposition"] = 'attachment; filename="055.jpg"'
  77. #1 可以文件名乱码
  78. #contentType, encoding = mimetypes.guess_type(file_name)
  79. #mainType, subType = contentType.split('/', 1)
  80. #file = open(file_name, 'rb')
  81. #attachment = MIMEBase(mainType, subType)
  82. #attachment.set_payload(file.read())
  83. #encode_base64(attachment)
  84. #file.close()
  85. #basename = os.path.basename(file_name);
  86. #attachment.add_header('Content-Disposition', 'attachment',filename=basename)
  87. #attachment.add_header('Content-Disposition', 'attachment',filename=os.path.basename(file_name)) #file_name
  88.  
  89. #2中文件名乱码
  90. #file_msg = MIMEApplication(open(file_name, 'rb').read()) #发文件
  91. #file_msg.add_header('Content-Disposition', 'attachment', filename="应用文写作.doc")
  92. #main_msg.attach(file_msg) #file_msg
  93.  
  94. #3注意:指定att的编码
  95. basename = os.path.basename(file_name)
  96. att = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8')
  97. att["Content-Type"] = 'application/octet-stream'
  98. att["Content-Disposition"] = 'attachment; filename=%s' % basename.encode("utf-8")
  99. main_msg.attach(att)
  100.  
  101. # 设置根容器属性
  102. main_msg['From'] = From
  103. if ReplyToMail!='none':
  104. main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail)
  105. main_msg['To'] = To;
  106. main_msg['Subject'] = subject;
  107. main_msg['Cc']=cc;
  108. main_msg['Bcc'] = bcc; #这无效
  109. main_msg['Date'] =formatdate(localtime=True)
  110. # 得到格式化后的完整文本
  111. fullText = main_msg.as_string()
  112. # 用smtp发送邮件
  113. try:
  114. server.sendmail(From, To.split(';'), fullText)
  115. finally:
  116. server.quit()
  117. #os.remove(file_name) 移除文件
  118. print("发送邮件成功");

  

  1. #4.中文无乱码
  2. file_name=u'F:/Python/应用文写作.doc'
  3. urlfilename="应用文写作.doc"
  4. att = MIMEBase('application', 'octet-stream')
  5. att.set_payload(open(file_name, 'rb').read())
  6. att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', urlfilename) ) #gbk 收件时,名称不乱码。
  7. encode_base64(att)
  8. main_msg.attach(att)

  

Python 3.4 send mail的更多相关文章

  1. SSIS Send Mail

    在SSIS中Send Mail的方法主要有三种,使用Send Mail Task,使用Script Task和使用存储过程msdb.dbo.sp_send_dbmail. 一,使用Send Mail ...

  2. mailsend - Send mail via SMTP protocol from command line

    Introduction mailsend is a simple command line program to send mail via SMTP protocol. I used to sen ...

  3. 发送邮件的三种方式:Send Mail Message

    发送邮件的三种方式: 1.VBS 执行vbs脚本文件的程序为: system32文件下的 NameSpace = "http://schemas.microsoft.com/cdo/conf ...

  4. How to attach multiple files in the Send Mail Task in SSIS

    Let’s say you need to create a SSIS package that creates 2 files and emails the files to someone. Yo ...

  5. python trojan development 1st —— use python to send mail and caputre the screen then combine them

    import smtplib from email.mime.text import MIMEText msg_from='1@qq.com' #发送方邮箱 passwd='bd' #填入发送方邮箱的 ...

  6. python trojan development 2nd —— use python to send mail and listen to the key board then combine them

    请勿用于非法用途!!!!!本人概不负责!!!原创作品,转载说明出处!!!!! from pynput.keyboard import Key,Listener import logging impor ...

  7. Send Mail using C# code

    using System.Net.Mail; public class MailHelp { public static void Send(string subject, string body) ...

  8. Python的生成器send()方法 & yield_from

    生成器对象是一个迭代器.但是它比迭代器对象多了一些方法,它们包括send方法,throw方法和close方法.这些方法,主要是用于外部与生成器对象的交互.本文先介绍send方法. send send方 ...

  9. golang:send mail using smtp package

    go语言发送邮件,可以使用smtp包,两个关键函数: func PlainAuth(identity, username, password, host string) Auth func SendM ...

随机推荐

  1. 如何在没有域的环境中搭建AlwaysOn(二)

    对DBA而言,不需要域就可以搭建SQL Server AlwaysOn是Windows Server 2016中最令人兴奋的功能了,它不仅可以降低搭建的成本,而且还减少了部署和运维的工作量. 上篇博客 ...

  2. Python调用C++的DLL

    import os import sys from ctypes import * test = cdll.LoadLibrary('D:\Python27\py.dll') print test.A ...

  3. C语言 · Interval · 求矩阵元素和

    问题描述 这里写问题描述. 输入格式 测试数据的输入一定会满足的格式. 例:输入的第一行包含两个整数n, m,分别表示矩阵的行数和列数.接下来n行,每行m个正整数,表示输入的矩阵. 输出格式 要求用户 ...

  4. 移动APP服务端设计开发注意要点

    2014年,移动APP的热度丝毫没有减退,怎么为您的移动端app设计良好的服务器端接口(API)呢? 下面谈谈我个人的一些想法. 2014年,移动APP的热度丝毫没有减退,并没有像桌面软件被WEB网站 ...

  5. 初学者--bootstrap(一)----在路上(1)

    bootstrap: 是目前最受欢迎的前端框架,并基于HTML.CSS.JavaScript 等,而且他是为了适应并兼容各个电子设备,是对媒体查询的封装. 1.什么是媒体查询   他 是响应式布局的方 ...

  6. 获取URL最后一个 ‘/’ 之后的字符

    在开发项目的过程中,经常遇到需要解析论坛,博客等的URL的问题,比如:'abc/def/jkl' 或 'abc/def/jkl/',获取最后一个‘/’之后的所有字符‘jkl’,由于特殊字符'/'的个数 ...

  7. 深入学习jQuery选择器系列第四篇——过滤选择器之属性选择器

    × 目录 [1]简单属性 [2]具体属性 [3]条件属性 前面的话 属性过滤选择器的过滤规则是通过元素的属性来获取相应的元素,对应于CSS中的属性选择器.属性过滤选择器可分为简单属性选择器.具体属性选 ...

  8. 平衡二叉树AVL删除

    平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...

  9. 平衡二叉树AVL插入

    平衡二叉树(Balancedbinary tree)是由阿德尔森-维尔斯和兰迪斯(Adelson-Velskiiand Landis)于1962年首先提出的,所以又称为AVL树. 定义:平衡二叉树或为 ...

  10. Python标准库13 循环器 (itertools)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在循环对象和函数对象中,我们了解了循环器(iterator)的功能.循环器是对象的 ...