#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 MVC开发基础学习笔记:三、Razor视图引擎、控制器与路由机制学习

    一.天降神器“剃须刀” — Razor视图引擎 1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留 ...

  2. 你get了无数技能,为什么一事无成

      前几日看到阮一峰老师的发的一句话,颇有感慨,「你只是坐在电脑前,往网上发表了一段文字或者一张图片,随便什么,就能够接触到多少陌生的灵魂.这就是我热爱互联网的原因」.我打心底认为这是一个最好的时代, ...

  3. .NET项目版本号的小随笔

    [题外话] 一直以来都对.NET项目中的几个版本号(AssemblyVersion.AssemblyFileVersion.AssemblyInformationalVersion)以及版本号中的Re ...

  4. 《Entity Framework 6 Recipes》中文翻译系列 (6) -----第二章 实体数据建模基础之使用Code First建模自引用关系

    2-5 使用Code First建模自引用关系 问题 你的数据库中一张自引用的表,你想使用Code First 将其建模成一个包含自关联的实体. 解决方案 我们假设你有如图2-14所示的数据库关系图的 ...

  5. Java 浅析三大特性之一继承

    上文Java 浅析三大特性之一封装我们说到Java是一个注重编写类,注重于代码和功能复用的语言.Java实现代码复用的方式有很多,这里介绍一个重要的复用方式--继承. 在介绍继承之前,我们要明确一点, ...

  6. 机器学习理论知识部分--偏差方差平衡(bias-variance tradeoff)

    摘要: 1.常见问题 1.1 什么是偏差与方差? 1.2 为什么会产生过拟合,有哪些方法可以预防或克服过拟合? 2.模型选择例子 3.特征选择例子 4.特征工程与数据预处理例子 内容: 1.常见问题 ...

  7. java反射 之 反射基础

    一.反射 反射:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为 ...

  8. DAC Usage2:通过DAC实现DB Schema的Migration和Upgrade

    一,Introduce Extract DAC 是从现存的DB中创建DAC,抽取DB Object的definition 和 与之相关的实例级别的元素,比如Login,以及Login 和User之间的 ...

  9. LINQ系列:Linq to Object量词操作符

    量词操作符返回一个Boolean值,指示序列中是否存在部分或全部元素符号指定条件.LINQ中量词操作符包括:All.Any和Contains. 1. All All操作符判定在集合中是否所有的值都满足 ...

  10. IOS入门之Swift语言(一)

    经过不断的努力,小哥也买了台苹果设备,终于可以开始我的IOS之旅了,说来确实令人苦恼,为了学习IOS我这着贫农阶级,省了几个月的零花钱,外加向亲朋好友求救,最终痛下心扉,卖了台MAC pro128G版 ...