码迷,mamicode.com
首页 > 编程语言 > 详细

使用python发送QQ邮件,以及添加附件

时间:2020-06-07 16:40:23      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:img   smtp   position   this   步骤   round   mamicode   file   isp   

这里以QQ邮箱为例子,显示步骤

打开邮箱依次点击--设置--账户,往下翻找到SMTP服务,点击打开即可,之后你会获得一串授权码,将它保存记录好,之后会用到

技术图片

 

 技术图片

 

 技术图片

首先我们发一条简单邮件

技术图片

import smtplib
from email.mime.text import MIMEText
sender = ‘#########@qq.com
receiver =‘##########@qq.com
psw = ‘############‘#你的授权码

content = "hey what‘s up man"#你的邮件内容
msg = MIMEText(content)#内容
msg[From] = who knows‘#发件人,真实发件人还是可以看到
msg[Subject] = just a test‘#邮件主体,标题
msg[To] = receiver#接受人

try:
    server = smtplib.SMTP_SSL(smtp.qq.com,465)#连接QQ邮箱的smtp服务器,和对应端口
    server.login(sender,psw)#登入你的账号
    server.sendmail(sender,receiver,msg.as_string())#从谁发送给谁,内容是什么
    server.quit()
    print("send already")
except:
    print("fail")

技术图片

你不写发件人就会显示出真实发送邮箱,写了就不会在预览中显示,但是点进去依旧看的到

 

同时也可以编写发送HTTP邮件

import smtplib
from email.mime.text import MIMEText
sender = ‘######@qq.com
receiver1 =‘#########@qq.com
psw = ##########
content = <html><body><h1>Hello World</h1></body></html>
msg = MIMEText(content,html,utf-8)
msg[From] = who knows
msg[To] = receiver1
msg[Subject] = whatever

try:
    s = smtplib.SMTP_SSL(smtp.qq.com,465)
    s.login(sender,psw)
    s.sendmail(sender,receiver1,msg.as_string())#以string发送
except:
    pass

 

将一个TXT内容发送给多个邮箱

import smtplib
from email.mime.text import MIMEText
sender = ‘########@qq.com
receiver1 =‘#########@qq.com
receiver2 = ‘########@qq.com
psw = ‘##########

with open(TXT.txt,r ,encoding=utf-8) as f:
    content = f.read()
    print(content)
msg = MIMEText(content)
msg[From] = who knows
msg[Subject] = just a test
try:
    server = smtplib.SMTP_SSL(smtp.qq.com,465)
    server.login(sender,psw)
    server.sendmail(sender,[receiver1,receiver2],msg.as_string())#我们将receiver变为一个列表即可实现向多个对象发送
    server.quit()
    print("send already")
except:
    print("fail")

 发送带附件的邮件

技术图片

 

 

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication

sender = ######@qq.com
receiver1 =#######@qq.com
psw = #########
content = this is a email with multipart
msg = MIMEText(content,html,utf-8)
msg[From] = who knows
msg[To] = receiver1
msg[Subject] = whatever


#发送图片附件
img = MIMEImage(open(dog.jpg,rb).read())
img.add_header(Content-Disposition, attachment, filename=dog.jpg)
#发送word附件
word = MIMEApplication(open(计算机网络.rtf,rb).read())
word.add_header(Content-Disposition, attachment, filename=计算机网络.rtf)
#发送一个TXT
TXT = MIMEApplication(open(TXT.txt,rb).read())
TXT.add_header(Content-Disposition, attachment, filename=TXT.txt)
#添加一个video
#但是接受到的时候文件是.bin后缀名,需要手动改成MP4后缀
# VIDEO = MIMEApplication(open(‘video.mp4‘,‘rb‘).read())
# VIDEO.add_header("Conten-Disposition",‘attachment‘,filename = ‘video.mp4‘)

multipart = MIMEMultipart()
multipart.attach(msg)
multipart.attach(img)
multipart.attach(word)
multipart.attach(TXT)
# multipart.attach(VIDEO)
try:
    s = smtplib.SMTP_SSL(smtp.qq.com,465)
    s.login(sender,psw)
    s.sendmail(sender,receiver1,multipart.as_bytes())
    print(succeed)
except:
    print(erro)

技术图片

 

 将图片放到正文中

#图片嵌入
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib

subject = A test with img
sender = #######@qq.com
receiver = #######@qq.com
psw = #########
msg = MIMEMultipart()
msg[Subject] = subject
msg[From] = sender
msg[To] = receiver
content = MIMEText(<html><body><img src="cid:img"  /></body></html>,html)#这里的cid:img代表引用那个ID为img的图片,当你有多张图片时命名多张
msg.attach(content)
img = MIMEImage(open(dog.jpg,rb).read())
img.add_header(Content-ID,img)#将ID命名为img
msg.attach(img)
try:
    s = smtplib.SMTP_SSL(smtp.qq.com,465)
    s.login(sender,psw)
    s.sendmail(sender,receiver,msg.as_string())#我测试的as_string和as_bytes结果是一样的
    print(succeed)
except:
    print(erro)

技术图片

 

 

 

 

 

优秀博客:https://blog.csdn.net/qq_20417499/article/details/80566265?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase 和 廖雪峰pytho教程SMTP篇

 

使用python发送QQ邮件,以及添加附件

标签:img   smtp   position   this   步骤   round   mamicode   file   isp   

原文地址:https://www.cnblogs.com/Truedragon/p/13044877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!