使用python发邮件

使用python发邮件,下面是代码,我使用的是88完美邮箱

该代码是我程序完成,然后它会自动发一封邮件,并且通过sys.argv将程序相关设置写到邮箱里。

下面代码是88邮箱为例:

python 复制代码
import sys
import smtplib
from email.header import Header
from email.mime.text import MIMEText

# 第三方 SMTP 服务
mail_host = "smtp.88.com"      # SMTP服务器
mail_user = "*****"                  # 用户名
mail_pass = "*****"               # 授权密码,非登录密码

# 邮箱设置
sender = 'your_mail_name@88.com'    # 发件人邮箱(最好写全, 不然会失败)
receivers = ['***@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# 邮件内容
content = str(sys.argv[1])  # 邮件内容
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)


if __name__ == '__main__':
    sendEmail()

下面是邮箱发送成功的截图:

相关推荐
rannn_11127 分钟前
【学以致用|python自动化办公】OCR批量识别自动存为Excel(批量识别发票)
python·ocr·excel·财务
AI视觉网奇1 小时前
pycharm 默认终端设置 cmd
ide·python·pycharm
言之。1 小时前
LiteLLM:让LLM调用变得简单统一
后端·python·flask
ZhengEnCi1 小时前
Python_try-except-finally 完全指南-从异常处理到程序稳定的 Python 编程利器
后端·python
jarreyer1 小时前
常见分析方法与对应图表汇总
python·信息可视化·数据分析
m0_64880493_江哥2 小时前
用正则方法从中英文本提取英文的python示例
python·mysql·正则表达式
N0nename2 小时前
TR3--Transformer之pytorch复现
人工智能·pytorch·python
Full Stack Developme2 小时前
jdk.random 包详解
java·开发语言·python
m***记2 小时前
Python 数据分析入门:Pandas vs NumPy 全方位对比
python·数据分析·pandas
MYX_3092 小时前
第七章 完整的模型训练
pytorch·python·深度学习·学习