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

Posted TrueDZ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python发送QQ邮件,以及添加附件相关的知识,希望对你有一定的参考价值。

这里以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邮件,以及添加附件的主要内容,如果未能解决你的问题,请参考以下文章

使用QQ邮箱发送邮件并添加附件

用python实现自动发邮件的功能

python 发送邮件,附件中文命名,怎么破

发送python电子邮件时添加excel文件附件

java发送邮件

Python 通过 SMTP 发送邮件