python发送邮件

python通过第三方代理方式发送邮件

# !/usr/bin/python
# -*- coding: UTF-8 -*-

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

# 第三方 SMTP 服务
mail_server = "smtp.163.com"  # 设置服务器
mail_port = 25
sender = "[email protected]"  # 用户名
sender_password = "xxx"  # 口令(授权码)
receivers = '[email protected]'  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

message = MIMEText('Python邮件发送测试...', 'plain', 'utf-8')
message['From'] = sender
message['To'] = receivers

send_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
subject = '邮件测试' + send_time
message['Subject'] = subject

print(message)

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_server, mail_port)  # 25 为 SMTP 端口号
    smtpObj.login(sender, sender_password)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException as e:
    print("Error: 无法发送邮件", e)

你可能感兴趣的:(linux,python,后端,python,后端)