python3 實戰項目系列之四

來源:互聯網
上載者:User

標籤:郵箱   ring   字串   授權   多個   from   點擊   user   工作   

Python 發送郵件

python發送郵件用到 smtplib 和 email 兩個包。我們需要下載安裝,採用brew install msmtp的方式就可以搞定了。

 

知識掃盲:

 

 

 

 

我們需要 開啟上述服務,操作步驟如下:

登陸郵箱-->設定-->POP3/SMTP/IMAP

點擊上述選項框,通過驗證碼的方式設定授權碼。一定要開啟上述服務,否則連不上。

 

 

 

 

準備工作做好了,接下來上代碼

# -*- coding:utf-8 -*-import smtplibfrom email.mime.text import MIMEText#郵箱地址email_host = ‘smtp.163.com‘# 寄件者帳號email_user = ‘[email protected]‘# 寄件者的密碼,這裡是???授權碼,授權碼,授權碼!重要的事說三遍!email_pwd = ‘xxx‘# 收件者的郵箱maillist =‘[email protected]‘me = email_user# 郵件內容msg = MIMEText(‘這個是Python 測試郵件,不用回複!‘)# 郵件主題msg[‘Subject‘] = ‘python測試‘# 寄件者帳號msg[‘From‘] = me# 接收者帳號列表,多個帳號可以用逗號隔開msg[‘To‘] = maillist# 串連郵箱,傳入郵箱地址,和連接埠號碼,smtp的連接埠號碼是25smtp = smtplib.SMTP(email_host, port=25)# 寄件者的郵箱帳號,密碼smtp.login(email_user, email_pwd)# 參數分別是寄件者,接收者,第三個是把上面的發送郵件的內容變成字串smtp.sendmail(me, maillist, msg.as_string())# 發送完畢後退出smtpsmtp.quit()print (‘email send success.‘)

 

返回??去查看剛才發送的郵件吧!

 

python3 實戰項目系列之四

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.