import smtplib from email.mime.text import MIMEText """ 发送邮件前的准备: 个人邮箱 如qq邮箱 163邮箱: 1. 去邮箱手动开启smtp服务 2. 得到授权码(登录使用) 企业邮箱: 已默认开启smtp, 登录密码使用账号密码即可 """ SenderConfig = { "user": "", # 发件人邮件地址 "password": "", # 密码 "server": "smtp.office365.com", # SMTP服务器地址 "port": 587 # SMTP服务器端口 } # 邮件接收人列表 ReceiverList = [ '' ] def login_mailbox(): """登录邮箱""" try: mail_sp = smtplib.SMTP_SSL(SenderConfig["server"], SenderConfig["port"]) mail_sp.login(SenderConfig["user"], SenderConfig["password"]) except Exception as e: raise ValueError("登录邮箱失败") return mail_sp def send_mail(msg): mail_sp = login_mailbox() msg = MIMEText(msg) try: mail_sp.sendmail(SenderConfig["user"], ReceiverList, msg.as_string()) except Exception as e: import traceback traceback.print_exc() return "邮件发送失败" finally: mail_sp.quit() return "邮件发送成功" if __name__ == "__main__": msg = 'ceshiceshi' res = send_mail(msg) print(res)
Python邮件发送
最新推荐文章于 2024-07-18 19:19:04 发布