用python發送郵件

# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Author: P♂boy
@License: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation Limited.
@Contact: [email protected]
@Software: Pycharm
@File: Email.py
@Time: 2019/1/10 16:23
@Desc:發送郵件測試
"""

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


def email():
    # 第三方 SMTP 服務
    mail_host = "smtp.qq.com"  # 設置服務器
    mail_user = "[email protected]"  # 用戶名
    mail_pass = "lodfhxkwjnjw****"  # 授權碼,可以在自己google如何申請對應郵箱的授權碼

    sender = '[email protected]'
    receivers = ['[email protected]']  # 接收郵件,可設置爲你的QQ郵箱或者其他郵箱

    message = MIMEText('Python 郵件發送測試...', 'plain', 'utf-8')
    message['From'] = Header("郭威", 'utf-8')
    message['To'] = Header("郭威", 'utf-8')

    subject = 'Python SMTP 郵件測試'
    message['Subject'] = Header(subject, 'utf-8')

    try:
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host, 25)  # 25 爲 SMTP 端口號
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print("郵件發送成功")
    except smtplib.SMTPException as e:
        print("Error: 無法發送郵件", e)

if __name__ == '__main__':
    email()

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