邮件传输协议(SMTP)
SSL
为Netscape所研发,用以保障在Internet上数据传输之安全,利用数据加密(Encryption)技术,可确保数据在网络上之传输过程中不会被截取及窃听。
SSL协议位于TCP/IP协议与各种应用层协议之间,为数据通讯提供安全支持。
以QQ邮箱为例子:开启smtp协议 和 一些设置
开启smtp协议 一些设置- [汉字编码报错] UnicodeEncodeError- 'ascii' codec can't encode characters in position 0-1- ordinal not in
- python 出现socket.gaierror: [Errno 11004] getaddrinfo failed错误
import smtplib
mail = ''
authorization_code = ''
send_mail = ''
# 服务的设置
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 登陆
server.login(mail, authorization_code)
# 发送信息
# 如果是汉字则
news = 'this is a news'
# 邮件的发送
server.sendmail(mail, send_mail, news)
# 退出
server.quit()
包装一下
import smtplib
from email.mime.text import MIMEText
mail = ''
authorization_code = ''
send_mail = ''
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 登陆
server.login(mail, authorization_code)
# 发送信息
# 如果是汉字则
news = 'this is a news'
# 邮件的发送
server.sendmail(mail, send_mail, str(MIMEText(news)))
# 退出
server.quit()
设置发送者和接收者
import smtplib
from email.mime.text import MIMEText
mail = ''
authorization_code = ''
send_mail = ''
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 登陆
server.login(mail, authorization_code)
# 发送信息
# 如果是汉字则
news = 'this is a news'
massage = MIMEText(news, 'plain', 'utf8')
# 设置主题
title = 'SMTP TEST'
# 设置邮件发送者和邮件接收者
massage['FROM'] = mail
massage['TO'] = send_mail
# 把主题加入到massage
massage['Subject'] = title
# 邮件的发送
server.sendmail(mail, send_mail, str(massage))
# 退出
server.quit()
附件
- Python_报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
- pdf打不开的问题
- ppt/word乱码的问题
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
mail = ''
authorization_code = ''
send_mail = ''
# 发送信息
news = 'this is a news'
massage = MIMEText(news, 'plain', 'utf8')
# 设置主题
title = 'SMTP TEST'
# 附件的部分
# content-disposition:保证图片是可以显示的
# attachment:保证已发送的附件是可以预览和下载的
image_path = 'preview.jpg'
image_part = MIMEImage(open(image_path, 'rb').read(), image_path.split('.')[-1])
# filename可以自定义
image_part.add_header('content-disposition', 'attachment', filename=image_path)
# 邮件的内容和附件进行拼接
m = MIMEMultipart()
m.attach(image_part)
m.attach(massage)
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 登陆
server.login(mail, authorization_code)
# 设置邮件发送者和邮件接收者
m['FROM'] = mail
m['TO'] = send_mail
# 把主题加入到massage
m['Subject'] = title
# 邮件的发送
server.sendmail(mail, send_mail, str(m))
# 退出
server.quit()
import smtplib
import os.path
from email import encoders
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
mail = ''
authorization_code = ''
send_mail = ''
# 发送信息
news = 'this is a news'
massage = MIMEText(news, 'plain', 'utf8')
# 设置主题
title = 'SMTP TEST'
# 附件的部分
# content-disposition:保证图片是可以显示的
# attachment:保证已发送的附件是可以预览和下载的
image_path = 'preview.jpg'
image_part = MIMEImage(open(image_path, 'rb').read(), image_path.split('.')[-1])
# filename可以自定义
image_part.add_header('content-disposition', 'attachment', filename=image_path)
# pdf的部分
pdf_path = r'E:\工具箱\身份资料\数据工程师.pdf'
pdf_part = MIMEApplication(open(image_path, 'rb').read(), image_path.split('.')[-1])
# filename可以自定义
pdf_part.add_header('content-disposition', 'attachment', filename='test.pdf')
# ppt
ppt_path = 'kylin基础.pptx'
ppt_part = MIMEApplication(open(image_path, 'rb').read(), image_path.split('.')[-1])
# filename可以自定义
ppt_part.add_header('content-disposition', 'attachment', filename='test.pptx')
# 邮件的内容和附件进行拼接
m = MIMEMultipart()
m.attach(image_part)
m.attach(massage)
# 添加多张图片则添加连接
m.attach(pdf_part)
m.attach(ppt_part)
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 登陆
server.login(mail, authorization_code)
# 设置邮件发送者和邮件接收者
m['FROM'] = mail
m['TO'] = send_mail
m['Accept-Language'] = 'zh-CN'
m['Accept-Charset'] = 'ISO-8859-1,utf-8'
# 把主题加入到massage
m['Subject'] = title
# 邮件的发送
server.sendmail(mail, send_mail, str(m))
# 退出
server.quit()
??乱码谁帮忙解决一下,PDF打不开??谁帮忙解决一下,网上查了好久全是......
网友评论