Python 3.4 send mail
#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的更多相关文章
- SSIS Send Mail
在SSIS中Send Mail的方法主要有三种,使用Send Mail Task,使用Script Task和使用存储过程msdb.dbo.sp_send_dbmail. 一,使用Send Mail ...
- 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 ...
- 发送邮件的三种方式:Send Mail Message
发送邮件的三种方式: 1.VBS 执行vbs脚本文件的程序为: system32文件下的 NameSpace = "http://schemas.microsoft.com/cdo/conf ...
- 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 ...
- 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' #填入发送方邮箱的 ...
- 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 ...
- Send Mail using C# code
using System.Net.Mail; public class MailHelp { public static void Send(string subject, string body) ...
- Python的生成器send()方法 & yield_from
生成器对象是一个迭代器.但是它比迭代器对象多了一些方法,它们包括send方法,throw方法和close方法.这些方法,主要是用于外部与生成器对象的交互.本文先介绍send方法. send send方 ...
- golang:send mail using smtp package
go语言发送邮件,可以使用smtp包,两个关键函数: func PlainAuth(identity, username, password, host string) Auth func SendM ...
随机推荐
- Android什么时候进行View中Background的加载
对大多数Android的开发者来说,最经常的操作莫过于对界面进行布局,View中背景图片的加载是最经常做的.但是我们很少关注这个过程,这篇文章主要解析view中背景图片加载的流程.了解view中背景图 ...
- Git 进阶指南
转自: https://github.com/kaiye/kaiye.github.com/issues/7 在掌握了基础的 Git 使用 之后,可能会遇到一些问题.以下是猫哥筛选总结的部分常见问题, ...
- 浅析MSIL中间语言——基础篇
一.开篇 研究MSIL纯属于个人喜好,说在前面MSIL应用于开发的地方很少,但是很大程度上能够帮着我们理解底层的原理,这是我了解MSIL的主要原因.托管代码表示应用程序的方法的功能,它们以微软的中间语 ...
- Windows Azure Storage (18) 使用HTML5 Portal的Azure CDN服务
<Windows Azure Platform 系列文章目录> Update:2015-04-15 如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的文档:Azu ...
- SqlServer2008到期升级企业版 密钥+图解
最近使用SQL Server2008,结果Sql Server Management Studio提示过期了,如图: 遇到如上图情况,需要将SQL Server2008升级维护下,还是输入原来的密钥就 ...
- thinkPHP入门之二
thinkphp提供了很多便利的方法,因为php要记的东西太多了,而thinkphp极大简化了这些,让编写过程更加快速. 为了预防那些了解它的人胡乱玩耍,thinkphp提供一个函数_empty 它是 ...
- VS代码提示不出现或者提示变成英文或者各种奇葩问题的解决
万能法==>重置 代码提示变成英文的了 打开开发员命令提示 输入:devenv.exe /setup /resetuserdata /resetsettings 重新打开,会和你第一次安装完毕一 ...
- .Net 转战 Android 4.4 日常笔记(10)--ADT集成环境更新SDK
今天下载了一份原来来参考,却发现SDK版本偏低我没有安装 用SDK Manager却一直更新不了出现 Failed to fetch URL https://dl-ssl.google.com/and ...
- iOS开发之SQLite--C语言接口规范(三)——Binding Values To Prepared Statements
在前面的博客中已经介绍了如何连接SQLite数据库,并且简单的查询和遍历结果集.在前面用到了sqlite3_stmt *stmt,也就是预编译后的SQL语句.在本篇博客中会了解一下sqlite3_st ...
- 小菜学习设计模式(三)—工厂方法(Factory Method)模式
前言 设计模式目录: 小菜学习设计模式(一)—模板方法(Template)模式 小菜学习设计模式(二)—单例(Singleton)模式 小菜学习设计模式(三)—工厂方法(Factory Method) ...