日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2025/3/20 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python发送邮件拒绝_人生苦短之Python发邮件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

#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ù)器及端口號

郵箱 發(fā)件服務(wù)器 非SSL協(xié)議端口 SSL協(xié)議端口

163 smtp.163.com 25 465/587

qq smtp.qq.com 25 465/587

發(fā)送郵件的幾個錯誤:

1.550錯誤(smtplib.SMTPAuthenticationError: (550, b'User has no permission'))

535錯誤(smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed'))

郵箱一般是不開啟客戶端授權(quán)的,所以登錄是拒絕的,需要去郵箱開啟,然后會發(fā)送短信

獲取授權(quán)碼作為客戶端登錄的密碼(login方法中的密碼)

2.503錯誤(503 ‘Error: A secure connection is requiered(such as ssl)’之類)

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

@from_addr 發(fā)送郵件的地址

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

@mail_host 郵箱的SMTP服務(wù)器地址

@mail_pass 郵箱開啟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)建郵件對象 MIMEMultipart 指定類型為 alternative可以支持同時發(fā)送html和plain,但是

# 不會都顯示,html優(yōu)先顯示

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()

#發(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

郵件信息的層級關(guān)系,詳細(xì)見https://docs.python.org/2/library/email.mime.html

'''

總結(jié)

以上是生活随笔為你收集整理的python发送邮件拒绝_人生苦短之Python发邮件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。