#coding=utf-8
#Python 3.4 https://docs.python.org/3.4/library/
#IDE:Visual Studio 2015 Window10
import atexit
import os
import unicodedata
import sys
import time
import unicodedata
import winsound
import code
import codecs
import math
import csv
import base64
import re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.utils import COMMASPACE, formatdate
import email
import os.path
import sys
import mimetypes
import configparser
import string #https://docs.python.org/3.4/library/email.html
#https://docs.python.org/3/library/email-examples.html inifile='F:/Python/055.JPG'
#config=ConfigParser.ConfigParser()
#config.read(inifile)
#os.remove(inifile) #移除文件
subject=Header("缔友计算机有限公司销售报告","utf-8") #邮件标题
ReplyToName="geovindu@dusystem.com"
ReplyToMail="geovindu@dusystem.com"
To="463588883@qq.com"
bcc="geovindu@163.com";
cc="geovindu@jw28.com";
COMMASPACE = ', ';
file_name=inifile #"file_name"
From = "%s<geovindu@dusystem.com>" % Header("缔友计算机信息有公司涂聚文","utf-8") #发件人和姓名
server = smtplib.SMTP("mail.dusystem.com",25)
server.login("geovindu@dusystem.com","888") #仅smtp服务器需要验证时 # 构造MIMEMultipart对象做为根容器
main_msg = MIMEMultipart("alternative"); #alternative related 2.x版本的email.MIMEMultipart.MIMEMultipart()
# 构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg =MIMEText("xxx帮你转发的邮件", _subtype='html',_charset="utf-8") #邮件内容 2.x版本的 email.MIMEText.MIMEText
main_msg.attach(text_msg) # 构造MIMEBase对象做为文件附件内容并附加到根容器 #图片未显示,附件不同
#fp = open(file_name, 'rb')
#msgImage = MIMEImage(fp.read())
#fp.close()
#msgImage.add_header('Content-ID', '<image1>')
#main_msg.attach(msgImage) ## 设置附件头
#basename = os.path.basename(file_name)
#file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
file_msg = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8') #发文件
file_msg["Content-Type"] = 'application/octet-stream'
file_msg["Content-Disposition"] = 'attachment; filename="055.JPG"' main_msg.attach(file_msg)
# 设置根容器属性
main_msg['From'] = From
if ReplyToMail!='none':
main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail)
main_msg['To'] = To;
main_msg['Subject'] = subject;
main_msg['Cc']=cc;
main_msg['Bcc'] = bcc; #这无效
main_msg['Date'] =formatdate(localtime=True)
# 得到格式化后的完整文本
fullText = main_msg.as_string()
# 用smtp发送邮件
try:
server.sendmail(From, To.split(';'), fullText)
finally:
server.quit()
#os.remove(file_name)
print("发送邮件成功");

  上发图片

下为发文件

#coding=utf-8
#Python 3.4 https://docs.python.org/3.4/library/
#IDE:Visual Studio 2015 Window10
import atexit #类库
import os
import unicodedata
import sys
import time
import unicodedata
import winsound
import code
import codecs
import math
import csv
import base64
import re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.encoders import encode_base64
from email.mime.application import MIMEApplication
from email.utils import COMMASPACE, formatdate
import email
import os.path
import sys
import mimetypes
import configparser
import string
import mimetypes
import base64; #https://docs.python.org/3.4/library/email.html
#https://docs.python.org/3/library/email-examples.html inifile=u'F:/Python/工应用文写作.doc'
#config=ConfigParser.ConfigParser()
#config.read(inifile)
#os.remove(inifile) #移除文件
subject=Header("缔友计算机有限公司销售报告","utf-8") #邮件标题
ReplyToName="geovindu@dusystem.com"
ReplyToMail="geovindu@dusystem.com"
To="463588883@qq.com"
bcc="geovindu@163.com";
cc="geovindu@jw28.com";
COMMASPACE = ', ';
file_name=inifile #"file_name"
From = "%s<geovindu@dusystem.com>" % Header("缔友计算机信息有公司涂聚文","utf-8") #发件人和姓名
server = smtplib.SMTP("mail.dusystem.com",25)
server.login("geovindu@dusystem.com","888") #仅smtp服务器需要验证时 # 构造MIMEMultipart对象做为根容器
main_msg = MIMEMultipart("alternative"); #alternative related 2.x版本的email.MIMEMultipart.MIMEMultipart()
# 构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg =MIMEText("请你及阅完文档,提出建议", _subtype='html',_charset="utf-8") #邮件内容 2.x版本的 email.MIMEText.MIMEText
main_msg.attach(text_msg) # 构造MIMEBase对象做为文件附件内容并附加到根容器 #图片未显示,附件不同
#fp = open(file_name, 'rb')
#msgImage = MIMEImage(fp.read())
#fp.close()
#msgImage.add_header('Content-ID', '<image1>')
#main_msg.attach(msgImage) ## 设置附件头
#basename = os.path.basename(file_name)
#file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
#图片
#file_image="F:/Python/055.jpg";
#file_msg = MIMEText(open(file_image, 'rb').read(), 'base64', 'utf-8') #发完文件不见了
#file_msg["Content-Type"] = 'application/octet-stream' # 图片(application/octet-stream)可以,但WORD不行
#file_msg["Content-Disposition"] = 'attachment; filename="055.jpg"'
#1 可以文件名乱码
#contentType, encoding = mimetypes.guess_type(file_name)
#mainType, subType = contentType.split('/', 1)
#file = open(file_name, 'rb')
#attachment = MIMEBase(mainType, subType)
#attachment.set_payload(file.read())
#encode_base64(attachment)
#file.close()
#basename = os.path.basename(file_name);
#attachment.add_header('Content-Disposition', 'attachment',filename=basename)
#attachment.add_header('Content-Disposition', 'attachment',filename=os.path.basename(file_name)) #file_name #2中文件名乱码
#file_msg = MIMEApplication(open(file_name, 'rb').read()) #发文件
#file_msg.add_header('Content-Disposition', 'attachment', filename="应用文写作.doc")
#main_msg.attach(file_msg) #file_msg #3注意:指定att的编码
basename = os.path.basename(file_name)
att = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename=%s' % basename.encode("utf-8")
main_msg.attach(att) # 设置根容器属性
main_msg['From'] = From
if ReplyToMail!='none':
main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail)
main_msg['To'] = To;
main_msg['Subject'] = subject;
main_msg['Cc']=cc;
main_msg['Bcc'] = bcc; #这无效
main_msg['Date'] =formatdate(localtime=True)
# 得到格式化后的完整文本
fullText = main_msg.as_string()
# 用smtp发送邮件
try:
server.sendmail(From, To.split(';'), fullText)
finally:
server.quit()
#os.remove(file_name) 移除文件
print("发送邮件成功");

  

#4.中文无乱码
file_name=u'F:/Python/应用文写作.doc'
urlfilename="应用文写作.doc"
att = MIMEBase('application', 'octet-stream')
att.set_payload(open(file_name, 'rb').read())
att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', urlfilename) ) #gbk 收件时,名称不乱码。
encode_base64(att)
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. 表格搞定 Asp.net Web 状态管理

    最近在网上搜罗了 ASP.NET WEB 状态管理方面的一些内容,终于把这些内容整合总结了一下. 1. 希望自己通过整理,能够掌握一些,为自己投资. 2. 以便自己忘记,又要浪费时间搜罗. 3. 希望 ...

  2. LVS原理与使用(1)

    负载均衡,无论是否真正了解过,但我相信所有跟编程打交道的读者都有听说.同时,它(负载均衡)也是被认为一个大型网站的标识性技术之一(但负载均衡的作用肯定不止这点用途).虽然网上也有不少关于LVS配置实用 ...

  3. 花一分钟来看看Worktile是如何为团队协作而生的

    团队协作,我们想的更深.更远.更多,花一分钟来看看我们特别奉献的故事,然后去注册一个账号,邀请小伙伴一起来工作,你会体会Worktile才是真正懂你的协作方式.

  4. maven package 知识(转载)

    “打包“这个词听起来比较土,比较正式的说法应该是”构建项目软件包“,具体说就是将项目中的各种文件,比如源代码.编译生成的字节码.配置文件.文档,按照规范的格式生成归档,最常见的当然就是JAR包和WAR ...

  5. linux添加自定义的命令!

    修改了/root/下的.bashrc -bash-4.1# vi .bashrc # .bashrc # User specific aliases and functions alias rm='r ...

  6. Java Thread 的 run() 与 start() 的区别

    Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别             1. ...

  7. 【非愚人】重要通知:04-01 贴吧继PHP资源之后又。。。

    贴吧继PHP资源之后又取消了JAVA,IOS等资源的贴,现在专注于Net,C++,Linux,平面设计.主要是为了让广大Net程序员具备全栈全平台牛人的潜力,故而取消那些干扰因素.Net的潜力和活力大 ...

  8. C#设计模式系列:访问者模式(Visitor)

    1.访问者模式简介 1.1>.定义 作用于某个对象群中各个对象的操作,可以使在不改变对象本身的情况下,定义作用于对象的新操作. 1.2>.使用频率   低 2.访问者模式结构 2.1> ...

  9. Autocomplete 自动补全(Webform实战篇)

    开篇语 因为项目中需要用到一个自动补全的功能,功能描述: 需求一:新增收件人的时候,自动下拉显示出数据库中所有的收件人信息(显示的信息包括:姓名-收件地址-联系方式) 需求二:选中一个值得时候,分别赋 ...

  10. Winform 生成不需要安装的exe可执行文件 ILMerge使用

    今天应领导要求,把一个程序打包生成一个可以执行的exe文件,不是安装包那种,类似于绿色文件,就是一个exe,可以直接运行.上网查了一下有一个工具可以实现ILMerge. 参照两个文档http://bl ...