python 发送邮件实例
文件形式的邮件
#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimetext import MIMEText from emailheader import Header sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***' msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 msg['Subject'] = Header(subject, 'utf-8') smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgas_string()) smtpquit()
HTML形式的邮件
#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimetext import MIMEText sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***' msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') msg['Subject'] = subject smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgas_string()) smtpquit()
带图片的HTML邮件
#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimemultipart import MIMEMultipart from emailmimetext import MIMEText from emailmimeimage import MIMEImage sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***' msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message' msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image<br><img src="/UploadFiles/2021-04-08/cid:image1">带附件的邮件
#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimemultipart import MIMEMultipart from emailmimetext import MIMEText from emailmimeimage import MIMEImage sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***' msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message' #构造附件 att = MIMEText(open('h:\\python\\jpg', 'rb')read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="jpg"' msgRootattach(att) smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgRootas_string()) smtpquit()群邮件
#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimetext import MIMEText sender = '***' receiver = ['***','****',……] subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***' msg = MIMEText('你好','text','utf-8') msg['Subject'] = subject smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgas_string()) smtpquit()各种元素都包含的邮件
#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimemultipart import MIMEMultipart from emailmimetext import MIMEText from emailmimeimage import MIMEImage sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***' # Create message container - the correct MIME type is multipart/alternative msg = MIMEMultipart('alternative') msg['Subject'] = "Link" # Create the body of the message (a plain-text and an HTML version) text = "Hi!\nHow are you" html = """\ <html> <head></head> <body> <p>Hi!<br> How are you"http://wwwpythonorg">link</a> you wanted </p> </body> </html> """ # Record the MIME types of both parts - text/plain and text/html part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') # Attach parts into message container # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred msgattach(part1) msgattach(part2) #构造附件 att = MIMEText(open('h:\\python\\jpg', 'rb')read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="jpg"' msgattach(att) smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgas_string()) smtpquit()基于SSL的邮件
#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimetext import MIMEText from emailheader import Header sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***' msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 msg['Subject'] = Header(subject, 'utf-8') smtp = smtplibSMTP() smtpconnect('smtpcom') smtpehlo() smtpstarttls() smtpehlo() smtpset_debuglevel(1) smtplogin(username, password) smtpsendmail(sender, receiver, msgas_string()) smtpquit()以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 第五街的士高《印度激情版》3CD [WAV+CUE][2.4G]
- 三国志8重制版哪个武将智力高 三国志8重制版智力武将排行一览
- 三国志8重制版哪个武将好 三国志8重制版武将排行一览
- 三国志8重制版武将图像怎么保存 三国志8重制版武将图像设置方法
- 何方.1990-我不是那种人【林杰唱片】【WAV+CUE】
- 张惠妹.1999-妹力新世纪2CD【丰华】【WAV+CUE】
- 邓丽欣.2006-FANTASY【金牌大风】【WAV+CUE】
- 饭制《黑神话》蜘蛛四妹手办
- 《燕云十六声》回应跑路:年内公测版本完成95%
- 网友发现国内版《双城之战》第二季有删减:亲亲环节没了!
- 邓丽君2024-《漫步人生路》头版限量编号MQA-UHQCD[WAV+CUE]
- SergeProkofievplaysProkofiev[Dutton][FLAC+CUE]
- 永恒英文金曲精选4《TheBestOfEverlastingFavouritesVol.4》[WAV+CUE]
- 群星《国风超有戏 第9期》[320K/MP3][13.63MB]
- 群星《国风超有戏 第9期》[FLAC/分轨][72.56MB]