python 使用 126 郵箱發送郵件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/4/21 23:12
# @Author  : xhzheng
# @Email   : [email protected]
# @File    : TestSendMail.py
# @Software: PyCharm

import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time


def sendMail(sender, receivers, mail_content):
    nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    # 三個參數:第一個爲文本內容,第二個 plain 設置文本格式,第三個 utf-8 設置編碼
    message = MIMEText(nowTime + '\n' + mail_content, 'plain', 'utf-8')
    message['Subject'] = Header("每日信息報告", 'utf-8')
    message['From'] = str("[email protected]")  # 發送者
    message['To'] = str("lucky every day")  # 接收者

    smtpObj = smtplib.SMTP()
    smtpObj.connect("smtp.126.com")
    try:
    	# 126的密碼需要自己去126的設置裏面查找
        smtpObj.login("[email protected]", "【126的密碼】")
    except:
        print("smtp login error")

    smtpObj.sendmail("[email protected]", receivers, message.as_string())
    smtpObj.quit()


if __name__ == '__main__':
    mail_content = "今天的天氣不錯,lucky!"
    receivers = ["[email protected]"]
    sender = "[email protected]"
    sendMail(sender, receivers, mail_content)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章