python实现发送邮件

python实现发送邮件,第1张

概述python实现发送邮件

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#_*_enCoding:utf-8_*_ #script for python3.2 #------------------------------------------------------------------------------- # name:        发送邮件 # Purpose: # # Author:      QiuChangJIE # # Created:     10/09/2012 # copyright:   (c) cj.qiu 2012 # licence:     <your licence> #-------------------------------------------------------------------------------  import osimport smtplibimport mimetypesfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email.mime.text import MIMETextfrom email.mime.audio import MIMEAudiofrom email.mime.image import MIMEImagefrom email.encoders import encode_base64 MAIL_163_USER = "[email protected]"MAIL_163_PWD = "test"MAIL_YEAH_USER = "[email protected]"MAIL_YEAH_PWD = "test"MAIL_Google_HOST = "smtp.gmail.com"MAIL_163_HOST = "smtp.163.com"MAIL_YEAH_HOST = "smtp.yeah.com" RECIPIENT = ["[email protected]"]ATTACHMENTS = [] class QMail():    def __init__(self,user,pwd,host):        self.mail_user = user        self.mail_pwd = pwd        self.mail_server = smtplib.SMTP()        self.mail_server.connect(host)        self.mail_server.ehlo()        self.mail_server.starttls()        self.mail_server.ehlo()        self.mail_server.login(self.mail_user,self.mail_pwd)     def __del__(self):        self.mail_server.close()     def send_mail(self,recipIEnt,subject,text,att_files=[]):        msg = MIMEMultipart()        msg["From"] = self.mail_user        msg["Subject"] = subject        msg["To"] = ",".join(recipIEnt)        msg.attach(MIMEText(text))        if len(att_files) > 0:            for file_name in att_files:                msg.attach(self.get_attachment(file_name))        self.mail_server.sendmail(self.mail_user,msg.as_string())     def get_attachment(self,file_name):        content_type,enCoding = mimetypes.guess_type(file_name)        if content_type is None or enCoding is not None:            content_type = "application/octet-stream"        main_type,sub_type = content_type.split('/',1)        file = open(file_name,"rb")        if main_type == "text":            attachment = MIMEText(file.read())        elif main_type == 'message':            attachment = email.message_from_file(file)        elif main_type == 'image':            attachment = MIMEImage(file.read(),_subType=sub_type)        elif main_type == 'audio':            attachment = MIMEAudio(file.read(),_subType=sub_type)        else:            attachment = MIMEBase(main_type,sub_type)        attachment.set_payload(file.read())        encode_base64(attachment)        file.close()        attachment.add_header('Content-disposition','attachment',filename=os.path.basename(file_name))        return attachment def test():    mail = QMail("[email protected]","test",MAIL_163_HOST)    mail.send_mail(["[email protected]"],"sub_test","text_test",r"G:\WorkSpace\Doing\CMMI文档模板.dot") if __name__ == '__main__':    test()

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的python实现发送邮件全部内容,希望文章能够帮你解决python实现发送邮件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1198884.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存