Python使用smtplib发送邮件

发布时间 2023-04-07 21:23:12作者: ZJ杰哥

源代码如下:
'import smtplib
import random
from email.mime.text import MIMEText

def Go_email(name,str):
mail_host = "smtp.sohu.com" #服务地址
mail_user = 'xxxxx'#邮件名称
mail_pass = 'xxxxxxxxxxxxx'#登录密码
sender = 'xxx@sohu.com'
receivers = [name]

#内容
message = MIMEText(str,'plain','utf-8')
# 邮件主题
message['Subject'] = 'xxx'#标题
message['From'] = sender
message['To'] = receivers[0]

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host,25)
    smtpObj.login(mail_user,mail_pass)

    smtpObj.sendmail(
        sender,receivers,message.as_string())

    smtpObj.quit()
    print('success')
    return True
except smtplib.SMTPException as e:
    print('error',e)
    return False

def To_email(name):
timp = Go_email(name, f"[正文内容]")'