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
'''
一些常用郵箱發(fā)件服務(wù)器及端口號(hào)
郵箱 發(fā)件服務(wù)器 非SSL協(xié)議端口 SSL協(xié)議端口
163 smtp.163.com 25 465/587
qq smtp.qq.com 25 465/587
發(fā)送郵件的幾個(gè)錯(cuò)誤:
1.550錯(cuò)誤(smtplib.SMTPAuthenticationError: (550, b'User has no permission'))
535錯(cuò)誤(smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed'))
郵箱一般是不開(kāi)啟客戶(hù)端授權(quán)的,所以登錄是拒絕的,需要去郵箱開(kāi)啟,然后會(huì)發(fā)送短信
獲取授權(quán)碼作為客戶(hù)端登錄的密碼(login方法中的密碼)
2.503錯(cuò)誤(503 ‘Error: A secure connection is requiered(such as ssl)’之類(lèi))
例如我們使用QQ郵箱是需要SSL登錄的,所以需要smtplib.SMTP()改成smtplib.SMTP_SSL()
@from_addr 發(fā)送郵件的地址
@to_addr 接收郵件的地址(可以是列表)
@mail_host 郵箱的SMTP服務(wù)器地址
@mail_pass 郵箱開(kāi)啟smtp 需要的授權(quán)碼
'''
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, 編碼) 創(chuàng)建郵件信息主體
msg['Subject'] 郵件主題
msg['From'] 郵件發(fā)送方
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()
#發(fā)送附件
def send_attachment_mail():
#創(chuàng)建郵件對(duì)象 MIMEMultipart 指定類(lèi)型為 alternative可以支持同時(shí)發(fā)送html和plain,但是
# 不會(huì)都顯示,html優(yōu)先顯示
msg = MIMEMultipart('alternative')
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = 'AttachmentMail'
# 郵件的正文還是MIMEText
part1 = MIMEText('這是個(gè)帶附件的郵件', 'plain', 'utf-8')
# 添加附件(添加一個(gè)本地的圖片)
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()
#發(fā)送帶圖片的文本郵件
def send_imagetext_mail():
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = 'ImagMail'
#創(chuàng)建展示圖片的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
郵件信息的層級(jí)關(guān)系,詳細(xì)見(jiàn)https://docs.python.org/2/library/email.mime.html
'''
總結(jié)
以上是生活随笔為你收集整理的python发送邮件拒绝_人生苦短之Python发邮件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql dml 日志_Oracle
- 下一篇: python语言中整数1010的二进制表