加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python 七种邮件内容发送方法实例

发布时间:2020-05-25 01:13:38 所属栏目:Python 来源:互联网
导读:Python 七种邮件内容发送方法实例

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader

sender='***'
receiver='***'
subject='pythonemailtest'
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()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText

sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'

msg=MIMEText('</pre>
<h1>你好</h1>
<pre>','html','utf-8')

msg['Subject']=subject

smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,msg.as_string())
smtp.quit()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.imageimportMIMEImage

sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'

msgRoot=MIMEMultipart('related')
msgRoot['Subject']='testmessage'

msgText=MIMEText('<b>Some<i>HTML</i>text</b>andanimage.
<imgalt=""src="cid:image1"/>
good!','utf-8')
msgRoot.attach(msgText)

fp=open('h:python1.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,msgRoot.as_string())
smtp.quit()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.imageimportMIMEImage

sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'

msgRoot=MIMEMultipart('related')
msgRoot['Subject']='testmessage'

#构造附件
att=MIMEText(open('h:python1.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,msgRoot.as_string())
smtp.quit()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText

sender='***'
receiver=['***','****',……]
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'

msg=MIMEText('你好',msg.as_string())
smtp.quit()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.imageimportMIMEImage

sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'

#Createmessagecontainer-thecorrectMIMEtypeismultipart/alternative.
msg=MIMEMultipart('alternative')
msg['Subject']="Link"

#Createthebodyofthemessage(aplain-textandanHTMLversion).
text="Hi!nHowareyou?nHereisthelinkyouwanted:nhttp://www.python.org"
html="""


Hi!

Howareyou?

Hereisthe<ahref="http://www.python.org">link</a>youwanted.



"""

#RecordtheMIMEtypesofbothparts-text/plainandtext/html.
part1=MIMEText(text,'plain')
part2=MIMEText(html,'html')

#Attachpartsintomessagecontainer.
#AccordingtoRFC2046,thelastpartofamultipartmessage,inthiscase
#theHTMLmessage,isbestandpreferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att=MIMEText(open('h:python1.jpg','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,msg.as_string())
smtp.quit()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'

msg=MIMEText('你好','utf-8')

smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username,msg.as_string())
smtp.quit()

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

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读