概述
需要用Python写一个发送邮件的,在B站上有教学视频
代码如下:有详细的注释,可以阅读修改,直接使用
代码
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
#设置SMIP服务
mailServer = "smtp.126.com"
mailUser = "2777890989"
mailPassword = "8870124230xx"
#发邮件
sender = ""
#收邮件
receivers = ""
#主题
title = "message"
#邮件内容 转化
msg = "hahha"
message = MIMEText(msg, 'plain', 'utf8')
#发送附件,图片
imagePath = "test.jpg"
imagePart = MIMEImage(open(imagePath, mode = 'rb').read(), imagePath.split('.')[-1])
imagePart.add_header("Content-Disposition", 'attachment' ,filename = imagePath)
#实例化一个对象,将内容拼接到发送信息里
m = MIMEMultipart()
m.attach(message)
m.attach(imagePart)
#邮件发送者 #邮件接受者 #标题
# message["from"] = "{}".format(sender)
# message["to"] = ','.join(receivers)
# message["Subject"] = title
m["from"] = "{}".format(sender)
m["to"] = ','.join(receivers)
m["Subject"] = title
#邮件的发送 登录 登录账号 ,发送内容
server = smtplib.SMTP_SSL(mailServer,"这里是端口")
server.login(mailUser, mailPassword)
server.sendmail(sender, receivers, m.as_string())
server.quit()