1.python 发送邮件,附件中文命名,怎么破
不知道你是不是用的smtp来发的,我的发中文的附件没问题 #coding=utf-8'''Created on 2014-11-03@author: Neo'''import smtplibfrom email.mime.text import MIMEText import email.mime.multipartfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEBase import MIMEBasefrom email import Encoders#你自己修改下xxx的部分mailto_list = ['xxx@xxx.com']mail_host = "smtp.xxx.com" # 设置服务器mail_user = "xxxx" # 用户名mail_pass = "xxxxx" # 口令 mail_postfix = "xxxx.com" # 发件箱的后缀def send_mail(): me = "Hello" + "<" + mail_user + "@" + mail_postfix + ">" # 这里的hello可以任意设置,收到信后,将按照设置显示 content = 'Plz get the attachment!' msg = MIMEMultipart() body = MIMEText(content, _subtype='html', _charset='gb2312') # 创建一个实例,这里设置为html格式邮件 msg.attach(body) msg['Subject'] = "Test" # 设置主题 msg['From'] = me msg['To'] = ";".join(mailto_list) part = MIMEBase('application', 'octet-stream') # 读入文件内容并格式化,此处文件为当前目录下,也可指定目录 例如:open(r'/tmp/123.txt','rb') part.set_payload(open(u'test中文.txt','rb').read()) Encoders.encode_base64(part) ## 设置附件头 part.add_header('Content-Disposition', u'attachment; filename="test中文.txt"') msg.attach(part) try: s = smtplib.SMTP() s.connect(mail_host) # 连接smtp服务器 s.login(mail_user, mail_pass) # 登陆服务器 s.sendmail(me, mailto_list, msg.as_string()) # 发送邮件 s.close() print 'send mail sucess' return True except Exception, e: print str(e) return False send_mail()测试,发送ok。
2.python 发送邮件,附件中文命名,怎么破
不知道你是不是用的smtp来发的,我的发中文的附件没问题
#coding=utf-8
'''
Created on 2014-11-03
@author: Neo
'''
import smtplib
from email.mime.text import MIMEText
import email.mime.multipart
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
#你自己修改下xxx的部分
mailto_list = ['xxx@xxx.com']
mail_host = "smtp.xxx.com" # 设置服务器
mail_user = "xxxx" # 用户名
mail_pass = "xxxxx" # 口令
mail_postfix = "xxxx.com" # 发件箱的后缀
def send_mail():
me = "Hello" + "<" + mail_user + "@" + mail_postfix + ">;" # 这里的hello可以任意设置,收到信后,将按照设置显示
content = 'Plz get the attachment!'
msg = MIMEMultipart()
body = MIMEText(content, _subtype='html', _charset='gb2312') # 创建一个实例,这里设置为html格式邮件
msg.attach(body)
msg['Subject'] = "Test" # 设置主题
msg['From'] = me
msg['To'] = ";".join(mailto_list)
part = MIMEBase('application', 'octet-stream')
# 读入文件内容并格式化,此处文件为当前目录下,也可指定目录 例如:open(r'/tmp/123.txt','rb')
part.set_payload(open(u'test中文.txt','rb').read())
Encoders.encode_base64(part)
## 设置附件头
part.add_header('Content-Disposition', u'attachment; filename="test中文.txt"')
msg.attach(part)
try:
s = smtplib.SMTP()
s.connect(mail_host) # 连接smtp服务器
s.login(mail_user, mail_pass) # 登陆服务器
s.sendmail(me, mailto_list, msg.as_string()) # 发送邮件
s.close()
print 'send mail sucess'
return True
except Exception, e:
print str(e)
return False
send_mail()测试,发送ok
3.如何用python发送email
python发邮件的库基于
import smtplib
from email.mime.text import MIMEText
from email.header import Header一般普通邮件的形式如下:
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
4.如何在python程序中发邮件
比如我用自己的 139 邮箱 lucia_gaga@139.com发送邮件到网易邮箱 也是我的办公邮箱 lu.han@beebank.com以下代码调试通过:# coding: utf-8import smtplibfrom email.mime.text import MIMETextfrom email.header import Headersender = 'lucia_gaga@139.com'receiver = 'lu.han@beebank.com'subject = 'python email test'smtpserver = 'smtp.139.com'username = 'lucia_gaga@139.com'password = 'xxxxxxxx'msg = MIMEText('你好 lucia 这是你的第一封 python 发出的邮件', 'text', 'utf-8') # 中文需参数‘utf-8',单字节字符不需要msg['Subject'] = Header(subject, 'utf-8')smtp = smtplib.SMTP()smtp.connect('smtp.139.com')smtp.login(username, password)smtp.sendmail(sender, receiver, msg.as_string())smtp.quit()运行结果:。
5.如何在python程序中发邮件
通过命令行发送邮件,功能强大 python ./mail -s $server -f $from -t $to -u $user -p $pass -S "$subject" -m "${mail_msg}" -F $file Python 发送邮件可以添加附件:#!/usr/bin/python from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email import Utils, Encoders import mimetypes, sys,smtplib,socket,getopt class SendMail:def __init__(self,smtp_server,from_addr,to_addr,user,passwd):self.mailserver=smtp_server self.from_addr=from_addr self.to_addr=to_addr self.username=user self.password=passwd def attachment(self,filename):fd=open(filename,'rb') filename=filename.split('/') mimetype,mimeencoding=mimetypes.guess_type(filename[-1]) if (mimeencoding is None) or (mimetype is None):mimetype='application/octet-stream' maintype,subtype=mimetype.split('/') if maintype=='text':retval=MIMEText(fd.read(), _subtype=subtype, _charset='utf-8') else:retval=MIMEBase(maintype,subtype) retval.set_payload(fd.read()) Encoders.encode_base64(retval) retval.add_header('Content-Disposition','attachment',filename=filename[-1]) fd.close() return retval def msginfo(self,msg,subject,filename):# message = """Hello, ALL#This is test message.#--Anonymous""" message=msg msg=MIMEMultipart() msg['To'] = self.to_addr msg['From'] = 'sa <'+self.from_addr+'>' msg['Date'] = Utils.formatdate(localtime=1) msg['Message-ID'] = Utils.make_msgid() if subject:msg['Subject'] = subject if message:body=MIMEText(message,_subtype='plain') msg.attach(body)#for filename in sys.argv[1:]:if filename:msg.attach(self.attachment(filename)) return msg.as_string() def send(self,msg=None,subject=None,filename=None):try:s=smtplib.SMTP(self.mailserver) try:s.login(self.username,self.password) except smtplib.SMTPException,e:print "Authentication failed:",e sys.exit(1) s.sendmail(self.from_addr,self.to_addr.split(','),self.msginfo(msg,subject,filename)) except (socket.gaierror,socket.error,socket.herror,smtplib.SMTPException),e:print "*** Your message may not have been sent!" print e sys.exit(2) else:print "Message successfully sent to %d recipient(s)" %len(self.to_addr) if __name__=='__main__':def usage():print """Useage:%s [-h] -s
."-F, --file= Attachment file name.-h, --help Help documen. """ %sys.argv[0] sys.exit(3) try:options,args=getopt.getopt(sys.argv[1:],"hs:f:t:u:p:S:m:F:","--help --server= --from= --to= --user= --pass= --subject= --msg= --file=",) except getopt.GetoptError:usage() sys.exit(3) server=None from_addr=None to_addr=None username=None password=None subject=None filename=None msg=None for name,value in options:if name in ("-h","--help"):usage() if name in ("-s","--server"):server=value if name in ("-f","--from"):from_addr=value if name in ("-t","--to"):to_addr=value if name in ("-u","--user"):username=value if name in ("-p","--pass"):password=value if name in ("-S","--subject"):subject=value if name in ("-m","--msg"):msg=value if name in ("-F","--file"):filename=value if server and from_addr and to_addr and username and password:test=SendMail(server,from_addr,to_addr,username,password) test.send(msg,subject,filename) else:usage()。
6.如何用Python发邮件
文件形式的邮件 [python] view plaincopy#!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***' msg = MIMEText('你好','text','utf-8')#中文需参数'utf-8',单字节字符不需要 msg['Subject'] = Header(subject, 'utf-8') smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() HTML形式的邮件[python] view plaincopy#!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***' msg = MIMEText('