Python學習筆記 發郵件

2021-09-07 18:07:22 字數 1769 閱讀 5965

參考:python3實現163郵箱smtp傳送郵件

1.首先需要註冊乙個網易的郵箱,開啟smtp服務,並使用其授權碼

2.傳送郵件的python指令碼

#!/usr/bin/python

# -*- coding: utf-8 -*-

import smtplib

from email.header import header

from email.mime.text import mimetext

# 第三方 smtp 服務

mail_host = "smtp.163.com" # smtp伺服器

mail_user = "***" # 使用者名稱

mail_pass = "***" # 授權密碼,非登入密碼

sender = "***@163.com" # 發件人郵箱(最好寫全, 不然會失敗)

receivers = ["***@126.com"] # 接收郵件,可設定為你的qq郵箱或者其他郵箱

content = '我用python'

title = '人生苦短' # 郵件主題

def sendemail():

message = mimetext(content, 'plain', 'utf-8') # 內容, 格式, 編碼

message['from'] = "{}".format(sender)

message['to'] = ",".join(receivers)

message['subject'] = title

try:

smtpobj = smtplib.smtp_ssl(mail_host, 465) # 啟用ssl發信, 埠一般是465

smtpobj.login(mail_user, mail_pass) # 登入驗證

smtpobj.sendmail(sender, receivers, message.as_string()) # 傳送

print("mail has been send successfully.")

except smtplib.smtpexception as e:

print(e)

def send_email2(smtp_host, from_account, from_passwd, to_account, subject, content):

email_client = smtplib.smtp(smtp_host)

email_client.login(from_account, from_passwd)

# create msg

msg = mimetext(content, 'plain', 'utf-8')

msg['subject'] = header(subject, 'utf-8') # subject

msg['from'] = from_account

msg['to'] = to_account

email_client.sendmail(from_account, to_account, msg.as_string())

email_client.quit()

if __name__ == '__main__':

sendemail()

# receiver = '***'

# send_email2(mail_host, mail_user, mail_pass, receiver, title, content)

python 發郵件 python發郵件

python提供smtplib模組,該模組定義了乙個smtp客戶端會話物件,可用於使用smtp或esmtp偵聽器守護程式向任何網際網路機器傳送郵件。這是乙個簡單的語法,用來建立乙個smtp物件,稍後將演示如何用它來傳送電子郵件 import smtplib smtpobj smtplib.smtp ...

Python 實現發郵件

usr bin python coding utf 8 import smtplib from email.mime.text import mimetext from email.header import header 第三方 smtp 服務 mail host smtp.163.com 設定伺...

Python自動發郵件

摘要 本文介紹如何使用python發郵件,主要原理是利用qq郵箱傳送郵件 獲得授權碼後將其寫在下面程式中,然後就可以給自己的郵箱發郵件了 使用qq郵箱傳送郵件 content是傳送的內容,格式為 hostusername你自己的qq郵箱名 tousername 接收方的郵箱賬號 import smt...