python发送图片到指定的邮箱

 
 
#! /usr/bin/env python
# coding=utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

receiver= "[email protected]"
sender = "[email protected]"
pwd= "xaxxxxxxxd"    #这里是[email protected]邮箱的授权码

msg = MIMEMultipart()
msg["Subject"] = "有陌生人来访!"    #邮件的主题
msg["From"] = sender
msg["To"] = receiver

part = MIMEText("请查收陌生人照片!") #邮件的正文
msg.attach(part)

# jpg类型附件
part = MIMEApplication(open('apple.jpg', 'rb').read())    #'apple.jpg'和该.py文件在同一个文件夹下
part.add_header('Content-Disposition', 'attachment', filename="apple.jpg")
msg.attach(part)

try:
    s = smtplib.SMTP("smtp.qq.com", timeout=30)  # 连接smtp邮件服务器,端口默认是25
    s.ehlo()
    s.starttls()
    s.login(sender, pwd)  # 登陆服务器
    s.sendmail(sender, receiver, msg.as_string())  # 发送邮件
    s.close()
    print('邮件发送成功!')
except smtplib.SMTPException:
    print('邮件发送失败!')

猜你喜欢

转载自blog.csdn.net/jiangsujiangjiang/article/details/80328814
今日推荐