本帖最后由 技术006 于 2023-9-20 17:04 编辑
[Python] 复制代码 import yagmail
class EmailSender:
def __init__(self, smtp_server, smtp_port, sender_email, sender_password):
self.smtp_server = smtp_server
self.smtp_port = smtp_port
self.sender_email = sender_email
self.sender_password = sender_password
self.yag = yagmail.SMTP(sender_email, sender_password, smtp_server, smtp_port)
def send_email(self, to, subject, contents):
try:
self.yag.send(to=to, subject=subject, contents=contents)
print("邮件发送成功")
except Exception as e:
print(f"Error sending email: {str(e)}")
# 创建 EmailSender 实例
email_sender = EmailSender(
# 邮件服务器,即smtp.163.com、smtp.qq.com等,根据实际情况填写
smtp_server="smtp.163.com",
# 邮件服务端口,普通协议端口25,SSL加密协议端口465
smtp_port=465,
# 发送邮箱地址
sender_email="*******@163.com",
# 发送邮箱授权码(各邮件运营商提供,不熟悉的百度)
sender_password="*******"
)
# 接收邮箱
to_email = "******@qq.com"
# 发送邮件的标题
email_subject = "金字塔"
# 发送邮件的内容
email_contents = "金字塔聚金成塔、日进斗金."
email_sender.send_email(to_email, email_subject, email_contents)
|