python 傳送郵箱

2021-10-04 17:48:01 字數 2501 閱讀 9080

#coding: utf-8    

import smtplib

from email.mime.multipart import mimemultipart

from email.mime.text import mimetext

from email.mime.image import mimeimage

from email.header import header

#設定smtplib所需的引數

#下面的發件人,收件人是用於郵件傳輸的。

smtpserver = 'smtp.163.com'

username = '***@163.com'

password='***'

sender='***@163.com'

#receiver='***@126.com'

#收件人為多個收件人

receiver=['***@126.com','***@126.com']

subject = 'python email test'

#通過header物件編碼的文字,包含utf-8編碼資訊和base64編碼資訊。以下中文名測試ok

#subject = '中文標題'

#subject=header(subject, 'utf-8').encode()

#構造郵件物件mimemultipart物件

#下面的主題,發件人,收件人,日期是顯示在郵件頁面上的。

msg = mimemultipart('mixed')

msg['subject'] = subject

msg['from'] = '***@163.com '

#msg['to'] = '***@126.com'

#收件人為多個收件人,通過join將列表轉換為以;為間隔的字串

msg['to'] = ";".join(receiver)

#msg['date']='2012-3-16'

#構造文字內容

text = "hi!\nhow are you?\nhere is the link you wanted:\n"

text_plain = mimetext(text,'plain', 'utf-8')

msg.attach(text_plain)

#構造鏈結

sendimagefile=open(r'd:\pythontest\testimage.png','rb').read()

image = mimeimage(sendimagefile)

image.add_header('content-id','')

image["content-disposition"] = 'attachment; filename="testimage.png"'

msg.attach(image)

#構造html

html = """

hi!how are you?

here is the link you wanted.

"""

text_html = mimetext(html,'html', 'utf-8')

text_html["content-disposition"] = 'attachment; filename="texthtml.html"'

msg.attach(text_html)

#構造附件

sendfile=open(r'd:\pythontest\1111.txt','rb').read()

text_att = mimetext(sendfile, 'base64', 'utf-8')

#以下附件可以重新命名成aaa.txt

#text_att["content-disposition"] = 'attachment; filename="aaa.txt"'

#另一種實現方式

text_att.add_header('content-disposition', 'attachment', filename='aaa.txt')

#以下中文測試不ok

#text_att["content-disposition"] = u'attachment; filename="中文附件.txt"'.decode('utf-8')

msg.attach(text_att)

#傳送郵件

smtp = smtplib.smtp()

smtp.connect('smtp.163.com')

#我們用set_debuglevel(1)就可以列印出和smtp伺服器互動的所有資訊。

#smtp.set_debuglevel(1)

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

python 爬蟲 郵箱傳送

import smtplib import time from email.header import header from email.mime.text import mimetext import requests from scrapy import selector 我用的是qq郵箱 a...

Python如何傳送郵箱演示

smtplib 用於郵件的發信動作 import smtplib from email.mime.text import mimetext email 用於構建郵件內容 發信方的資訊 發信郵箱,qq 郵箱授權碼 from addr 1334325679 qq.com password cfyrgop...

python實現126郵箱傳送郵件

用python傳送126郵件,供大家參考,具體內容如下 今天想做個自動化郵件提醒的功能,最近剛好在學習python,都說python那麼強大,想試一下python能否搞定,搜一下資料,果真可以,而且又簡單通俗易懂 from email.mail.text import mimetext import...