Python邮件发送案例
生活随笔
收集整理的這篇文章主要介紹了
Python邮件发送案例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
發(fā)送郵件:
發(fā)送郵件的幾個(gè)要素:內(nèi)容:發(fā)件人(發(fā)件人的名稱、發(fā)件人的郵箱地址)收件人(收件人的名稱、收件人的郵箱地址)郵件的名稱郵件內(nèi)容(文件郵件(文字和圖片)、HTML郵件)服務(wù)器:smtp服務(wù)器地址:smtp.toto.cn賬戶:xxxx密碼:**** 發(fā)送郵件的包:smtplib郵件案例的代碼:
import smtplib from email.header import Header from email.mime.text import MIMEText from email.utils import formataddrpasswd = "mxy123456" """ 發(fā)送郵件的幾個(gè)要素:內(nèi)容:發(fā)件人(發(fā)件人的名稱、發(fā)件人的郵箱地址)收件人(收件人的名稱、收件人的郵箱地址)郵件的名稱郵件內(nèi)容(文件郵件(文字和圖片)、HTML郵件)服務(wù)器:smtp服務(wù)器地址:smtp.toto.cn賬戶:xxxx密碼:**** 發(fā)送郵件的包:smtplib """from_name = "9527" from_mail = "toto@toto.cn"to_name = "張三" to_mail = "toto@toto.cn"title ="fddfsafdsafdsafd" content = "愛(ài)我中華愛(ài)我中華愛(ài)我中華愛(ài)我中華愛(ài)我中華"smtp_addr = "smtp.toto.cn" user = "maoxiangyi@toto.cn" passwd = passwdmsg = MIMEText(content,"plain",_charset="utf-8") msg['Subject'] = Header(title) msg['From'] = formataddr((from_name,from_mail),charset='utf-8') msg['To']=formataddr((to_name,to_mail),charset='utf-8')#導(dǎo)入 發(fā)郵件的包 #創(chuàng)建服務(wù) server = smtplib.SMTP(host=smtp_addr,port=25) server.login(user=user,password =passwd) server.sendmail(formataddr((from_name,from_mail),charset='utf-8'),formataddr((to_name,to_mail),charset='utf-8'),msg.as_string()) #發(fā)送之后退出 server.quit()郵件接收端代碼:
import poplib from email.parser import Parser from email.utils import parseaddrpwd = "mxy123456"""" 1、準(zhǔn)備接受服務(wù)器的地址 Pop.toto.cn 2、賬戶密碼 toto@toto.cn Xxxx 3、類庫(kù):poplib 4、登錄 5、獲取數(shù)據(jù) ----------------------------- 將數(shù)據(jù)解析成郵件 """pop_server = "pop.toto.cn" user = "toto@toto.cn" pwd = pwdserver = poplib.POP3(pop_server) server.user(user) server.pass_(pwd) #通過(guò)stat方法,獲取服務(wù)中的郵件信息 data = server.stat() print(data) mail = server.retr(30) #轉(zhuǎn)換數(shù)據(jù) content = b'\r\n'.join(mail[1]).decode("utf-8") print(content) msg = Parser().parsestr(content) print(msg.get("Subject")) print(parseaddr(msg.get("From"))) print(parseaddr(msg.get("To")).decode("utf-8"))總結(jié)
以上是生活随笔為你收集整理的Python邮件发送案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python中的urllib,urlli
- 下一篇: 像虾仁的是什么东西?