欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python发送邮件拒绝_人生苦短之Python发邮件

发布时间:2025/3/20 python 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python发送邮件拒绝_人生苦短之Python发邮件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

#coding=utf-8

import smtplib

from email.mime.base import MIMEBase

from email.mime.image import MIMEImage

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

'''

一些常用邮箱发件服务器及端口号

邮箱 发件服务器 非SSL协议端口 SSL协议端口

163 smtp.163.com 25 465/587

qq smtp.qq.com 25 465/587

发送邮件的几个错误:

1.550错误(smtplib.SMTPAuthenticationError: (550, b'User has no permission'))

535错误(smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed'))

邮箱一般是不开启客户端授权的,所以登录是拒绝的,需要去邮箱开启,然后会发送短信

获取授权码作为客户端登录的密码(login方法中的密码)

2.503错误(503 ‘Error: A secure connection is requiered(such as ssl)’之类)

例如我们使用QQ邮箱是需要SSL登录的,所以需要smtplib.SMTP()改成smtplib.SMTP_SSL()

@from_addr 发送邮件的地址

@to_addr 接收邮件的地址(可以是列表)

@mail_host 邮箱的SMTP服务器地址

@mail_pass 邮箱开启smtp 需要的授权码

'''

from_addr = '331957324@qq.com'

to_addr = '252624008@qq.com'

mail_host = 'smtp.qq.com'

mail_pass = 'itrwvjhjxupgbhhc'

#文本形式的邮件

def send_text_mail():

try:

'''

MIMETest(content, type, 编码) 创建邮件信息主体

msg['Subject'] 邮件主题

msg['From'] 邮件发送方

msg['To'] 收件方

'''

msg = MIMEText('hello send by python', 'plain', 'utf-8')

msg['From'] = from_addr

msg['To'] = ','.join(to_addr)

msg['Subject'] = '主题'

server = smtplib.SMTP_SSL(mail_host, 465)

server.login(from_addr, mail_pass)

server.sendmail(from_addr, [to_addr, ], msg.as_string())

except Exception as e:

print e

#HTML格式的邮件

def send_html_mail():

msg = MIMEText('

你好

', 'html', 'utf-8')

msg['Subject'] = 'html'

smtp = smtplib.SMTP_SSL(mail_host, 465)

smtp.login(from_addr, mail_pass)

smtp.sendmail(from_addr, to_addr, msg

.as_string())

smtp.quit()

#发送附件

def send_attachment_mail():

#创建邮件对象 MIMEMultipart 指定类型为 alternative可以支持同时发送html和plain,但是

# 不会都显示,html优先显示

msg = MIMEMultipart('alternative')

msg['From'] = from_addr

msg['To'] = to_addr

msg['Subject'] = 'AttachmentMail'

# 邮件的正文还是MIMEText

part1 = MIMEText('这是个带附件的邮件', 'plain', 'utf-8')

# 添加附件(添加一个本地的图片)

att1 = MIMEText(open("C:\\6.jpg", 'rb').read(), 'base64', 'utf-8')

att1['Content-Type'] = 'application/octet-stream'

att1['Content-Disposition'] = 'attachment;filename="6.jpg"'

att1['Content-ID'] = '<0>'

msg.attach(att1)

msg.attach(part1)

smtp = smtplib.SMTP_SSL(mail_host, 465)

smtp.login(from_addr, mail_pass)

smtp.sendmail(from_addr, to_addr, msg

.as_string())

smtp.quit()

#发送带图片的文本邮件

def send_imagetext_mail():

msg = MIMEMultipart()

msg['From'] = from_addr

msg['To'] = to_addr

msg['Subject'] = 'ImagMail'

#创建展示图片的html

msg_html = MIMEText('Some HTML text and an image.
good!',

'html', 'utf-8')

msg.attach(msg_html)

#添加图片模块

fp = open('C:\\6.jpg', 'rb')

msg_image = MIMEImage(fp.read())

fp.close()

msg_image.add_header('Content-ID', '')

msg.attach(msg_image)

smtp = smtplib.SMTP_SSL(mail_host, 465)

smtp.login(from_addr, mail_pass)

smtp.sendmail(from_addr, to_addr, msg

.as_string())

smtp.quit()

send_imagetext_mail()

'''

Message

+- MIMEBase

+- MIMEMultipart

+- MIMENonMultipart

+- MIMEMessage

+- MIMEText

+- MIMEImage

邮件信息的层级关系,详细见https://docs.python.org/2/library/email.mime.html

'''

总结

以上是生活随笔为你收集整理的python发送邮件拒绝_人生苦短之Python发邮件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。