python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)
来源:互联网 发布:bj单身日记 知乎 编辑:程序博客网 时间:2024/06/05 18:25
[python] view plain copy
- #!/usr/bin/env
python3 - #coding:
utf-8 - import
smtplib - from
email.mime.text importMIMEText - from
email.header importHeader -
- sender
= '***' - receiver
= '***' - subject
= 'python email test' - smtpserver
= 'smtp.163.com' - username
= '***' - password
= '***' -
- msg
= MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要 - msg['Subject']
= 'utf-8')Header(subject, -
- smtp
= smtplib.SMTP() - smtp.connect('smtp.163.com')
- smtp.login(username,
password) - smtp.sendmail(sender,
receiver, msg.as_string()) - smtp.quit()
HTML形式的邮件
[python] view plain copy
- #!/usr/bin/env
python3 - #coding:
utf-8 - import
smtplib - from
email.mime.text importMIMEText -
- sender
= '***' - receiver
= '***' - subject
= 'python email test' - smtpserver
= 'smtp.163.com' - username
= '***' - password
= '***' -
- msg
= MIMEText(' 你好
','html','utf-8') -
- msg['Subject']
= subject -
- smtp
= smtplib.SMTP() - smtp.connect('smtp.163.com')
- smtp.login(username,
password) - smtp.sendmail(sender,
receiver, msg.as_string()) - smtp.quit()
带图片的HTML邮件
[python] view plain copy
- #!/usr/bin/env
python3 - #coding:
utf-8 - import
smtplib - from
email.mime.multipart importMIMEMultipart - from
email.mime.text importMIMEText - from
email.mime.image importMIMEImage -
- sender
= '***' - receiver
= '***' - subject
= 'python email test' - smtpserver
= 'smtp.163.com' - username
= '***' - password
= '***' -
- msgRoot
= MIMEMultipart('related') - msgRoot['Subject']
= 'testmessage' -
- msgText
= MIMEText('Some HTML text and ,'html','utf-8')an image.
good!' - msgRoot.attach(msgText)
-
- fp
= open('h:\\python\\1.jpg', 'rb') - msgImage
= MIMEImage(fp.read()) - fp.close()
-
- msgImage.add_header('Content-ID',
'') - msgRoot.attach(msgImage)
-
- smtp
= smtplib.SMTP() - smtp.connect('smtp.163.com')
- smtp.login(username,
password) - smtp.sendmail(sender,
receiver, msgRoot.as_string()) - smtp.quit()
[python] view plain copy
- #!/usr/bin/env
python3 - #coding:
utf-8 - import
smtplib - from
email.mime.multipart importMIMEMultipart - from
email.mime.text importMIMEText - from
email.mime.image importMIMEImage -
- sender
= '***' - receiver
= '***' - subject
= 'python email test' - smtpserver
= 'smtp.163.com' - username
= '***' - password
= '***' -
- msgRoot
= MIMEMultipart('related') - msgRoot['Subject']
= 'testmessage' -
- #构造附件
- att
= MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') - att["Content-Type"]
= 'application/octet-stream' - att["Content-Disposition"]
= 'attachment;filename="1.jpg"' - msgRoot.attach(att)
-
- smtp
= smtplib.SMTP() - smtp.connect('smtp.163.com')
- smtp.login(username,
password) - smtp.sendmail(sender,
receiver, msgRoot.as_string()) - smtp.quit()
群邮件
[python] view plain copy
- #!/usr/bin/env
python3 - #coding:
utf-8 - import
smtplib - from
email.mime.text importMIMEText -
- sender
= '***' - receiver
= ['***','****',……] - subject
= 'python email test' - smtpserver
= 'smtp.163.com' - username
= '***' - password
= '***' -
- msg
= MIMEText('你好','plain','utf-8') -
- msg['Subject']
= subject -
- smtp
= smtplib.SMTP() - smtp.connect('smtp.163.com')
- smtp.login(username,
password) - smtp.sendmail(sender,
receiver, msg.as_string()) - smtp.quit()
各种元素都包含的邮件
[python] view plain copy
- #!/usr/bin/env
python3 - #coding:
utf-8 - import
smtplib - from
email.mime.multipart importMIMEMultipart - from
email.mime.text importMIMEText - from
email.mime.image importMIMEImage -
- sender
= '***' - receiver
= '***' - subject
= 'python email test' - smtpserver
= 'smtp.163.com' - 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?\nHere is the link you wanted:\nhttp://www.python.org" - html
= """\ -
-
-
-
Hi!
-
How are you? -
Here is the link you wanted. -
-
-
- """
-
- #
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. - msg.attach(part1)
- msg.attach(part2)
- #构造附件
- att
= MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') - att["Content-Type"]
= 'application/octet-stream' - att["Content-Disposition"]
= 'attachment;filename="1.jpg"' - msg.attach(att)
-
- smtp
= smtplib.SMTP() - smtp.connect('smtp.163.com')
- smtp.login(username,
password) - smtp.sendmail(sender,
receiver, msg.as_string()) - smtp.quit()
基于SSL的邮件
[python] view plain copy
- #!/usr/bin/env
python3 - #coding:
utf-8 - import
smtplib - from
email.mime.text importMIMEText - from
email.header importHeader - sender
= '***' - receiver
= '***' - subject
= 'python email test' - smtpserver
= 'smtp.163.com' - username
= '***' - password
= '***' -
- msg
= MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要 - msg['Subject']
= 'utf-8')Header(subject, -
- smtp
= smtplib.SMTP() - smtp.connect('smtp.163.com')
- smtp.ehlo()
- smtp.starttls()
- smtp.ehlo()
- smtp.set_debuglevel(1)
- smtp.login(username,
password) - smtp.sendmail(sender,
receiver, msg.as_string()) - smtp.quit()
- 转载 原文地址:
- http://blog.sina.com.cn/s/blog_77ca69890101eo9x.html
qq、126邮箱都用授权码
qq邮箱要用ssl
0 0
- python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)
- python发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)
- yii2 发邮件 html 附件 文本等
- javamail 发送邮件,发复杂html,包括附件,以及内嵌图片的邮件
- spring简单邮件发送(文本/html/图片/附件)
- JavaMail实现邮件发送的简单封装(包括文本、图片、html、附件)
- Python3.5 email发送邮件,包含txt、图片、HTML、附件
- javamail发送文本邮件、HTML邮件及带附件邮件
- python发送邮件的实例代码(支持html、图片、附件)
- zend_mail实现发送邮件实例(包括附件)
- python应用系列教程——python使用smtp协议发送邮件:html文本邮件、图片邮件、文件附件邮件
- perl发html+附件的邮件
- php 发邮件 带附件 html
- 使用javaxmail发送邮件(带/不带附件,文本邮件/html邮件)
- php发送邮件 可发html格式邮件 附件
- java mail发送复杂邮件代码(包括附件,html消息,纯文本消息)
- javamail 发邮件 附件
- python3 发邮件实例(…
- Java实现单例模式之饿汉式、懒汉式、枚举式,带测试。
- 算法导论——双调欧几里得旅行商问题
- 剑指offer-二叉树的镜像
- 规则和统计(上)
- Qt Mediaplayer
- python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)
- validate.js使用方法
- What is “origin” in Git?
- C语言判断素数
- 【linux】centOS firewall添加开放端口
- CSDN日报20170505 ——《创业时该不该用新手程序员》
- tensorflow 实践(一)使用神经网络做中文情感分析
- linux命令指南之四
- Unity自定义创建脚本模板菜单