有思俱乐部学习园地

网站监测调用1473邮件系统


使用Python连接到内网mail.1473.cn的587端口即可发送邮件

# 引用smtp服务,用于发送邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header

def sendmail():  # 发送邮件
    # ###连接smtp服务器###
    # 创建smtp连接
    smtp = smtplib.SMTP()
    # 设置smtp服务器
    smtp.connect('mail.1473.cn', 587)
    smtp.ehlo()
    smtp.starttls()
    # 使用邮箱密码登录
    try:
        # 尝试发送邮件
        smtp.login('postmaster@mail.1473.cn', 'password')
    except smtplib.SMTPNotSupportedError as e:
        # 报错输出错误
        print(e)
        return False

    # 定义邮箱内容
    message = MIMEText("Hello, world!", 'plain', 'utf-8')
    # 定义邮箱标题
    message['Subject'] = Header('发送邮件测试', 'utf-8')
    # 定义邮箱发件人
    message['From'] = Header('postmaster@mail.1473.cn', 'utf-8')
    # 定义邮箱接收人
    message['To'] = Header('Receiver', 'utf-8')
    try:
        # 发送邮件
        smtp.sendmail('postmaster@mail.1473.cn', ['test@1473.cn'], message.as_string())
        return True
    except smtplib.SMTPException as e:
        # 输出错误
        print(e)
        return False


sendmail()

工作人员

 
作者:
信息录入: