python3 傳送任意檔案郵件的例項

2022-10-04 20:27:23 字數 1378 閱讀 2256

例項如下所示:

#!/usr/bin/python

# -*- coding: utf-8 -*-

import smtplib

import email.mime.multipart

import email.mime.text

from email.mime.text import mimetext

from email.mime.multi程式設計客棧part import mimemultipart

from email.mime.application import mimeapplication

def send_ewww.cppcns.commail(smtphost, sendaddr, password, recipientaddrs, subject='', content=''):

msg = email.mime.multipart.mimemultipart()

msg['from'] = sendaddr

msg['to'] = recipientaddrs

msg['subject'] = subject

content = content

txt = email.mime.text.mimetext(content, 'plain', 'utf-8')

msg.attach(txt)

# 新增附件,傳送d:/mydev/yasuo.rar檔案

part = mimeapplication(open('d:/mydev/6.rar','rb').read())

part.add_header('content-disposition', 'attachment', filename="yasuo.rar")

msg.attach(part)

smtp = smtplib.smtp()

smtp.connect(smtphost, '25')

smtp.login(sendaddr, password)

smtp.sendmail(sendaddr, recipientaddrs, str(msg))

print("傳送成功!")

smtp.quit()

try:

subject = 'py 測試郵件'

content = '這是一封來自 python 編寫的測試郵件。'

send_email('smtp.163.com', '183101617程式設計客棧[email protected]', '郵箱密碼', '[email protected]', subject, content)

except exception as err:

print(err)

本文標題: python3 傳送任意檔案郵件的例項

本文位址:

關於python3 傳送郵件

from email.mime.text import mimetext from email.header import header from smtplib import smtp ssl qq伺服器 host server smtp.qq.com sender qq為發件人的qq號碼 sen...

Python3 傳送郵件踩坑記錄

新手學習python3.5,根據教程試一試使用python3.5中的smtplib模組傳送電子郵件。出現多個問題 1 error 無法傳送郵件.case 550,b user has no permission 和 error 無法傳送郵件.case 535,b error authenticati...

利用Python3傳送郵件 親測可行

1 修改pip源為國內源頭mkdir pip vim pip pip.conf 寫入下面的內容 global index url install trusted host mirrors.aliyun.com2 安裝發郵件的包pip3 install pyemail3 郵箱配置開啟smtp 進入郵箱...