python發郵件詳解_python實現發送郵件詳解

[Python]代碼

#_*_encoding:utf-8_*_

#script for python3.2

#-------------------------------------------------------------------------------

# Name: 發送郵件

# Purpose:

#

# Author: QiuChangJie

#

# Created: 10/09/2012

# Copyright: (c) cj.qiu 2012

# Licence:

#-------------------------------------------------------------------------------

import os

import smtplib

import mimetypes

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

from email.mime.text import MIMEText

from email.mime.audio import MIMEAudio

from email.mime.image import MIMEImage

from email.encoders import encode_base64

MAIL_163_USER = "[email?protected]"

MAIL_163_PWD = "test"

MAIL_YEAH_USER = "[email?protected]"

MAIL_YEAH_PWD = "test"

MAIL_GOOGLE_HOST = "smtp.gmail.com"

MAIL_163_HOST = "smtp.163.com"

MAIL_YEAH_HOST = "smtp.yeah.com"

RECIPIENT = ["[email?protected]"]

ATTACHMENTS = []

class QMail():

def __init__(self, user, pwd, host):

self.mail_user = user

self.mail_pwd = pwd

self.mail_server = smtplib.SMTP()

self.mail_server.connect(host)

self.mail_server.ehlo()

self.mail_server.starttls()

self.mail_server.ehlo()

self.mail_server.login(self.mail_user, self.mail_pwd)

def __del__(self):

self.mail_server.close()

def send_mail(self, recipient, subject, text, att_files=[]):

msg = MIMEMultipart()

msg["From"] = self.mail_user

msg["Subject"] = subject

msg["To"] = ",".join(recipient)

msg.attach(MIMEText(text))

if len(att_files) > 0:

for file_name in att_files:

msg.attach(self.get_attachment(file_name))

self.mail_server.sendmail(self.mail_user, recipient, msg.as_string())

def get_attachment(self, file_name):

content_type, encoding = mimetypes.guess_type(file_name)

if content_type is None or encoding is not None:

content_type = "application/octet-stream"

main_type, sub_type = content_type.split('/', 1)

file = open(file_name, "rb")

if main_type == "text":

attachment = MIMEText(file.read())

elif main_type == 'message':

attachment = email.message_from_file(file)

elif main_type == 'image':

attachment = MIMEImage(file.read(), _subType=sub_type)

elif main_type == 'audio':

attachment = MIMEAudio(file.read(), _subType=sub_type)

else:

attachment = MIMEBase(main_type, sub_type)

attachment.set_payload(file.read())

encode_base64(attachment)

file.close()

attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_name))

return attachment

def test():

mail = QMail("[email?protected]", "test", MAIL_163_HOST)

mail.send_mail(["[email?protected]"], "sub_test", "text_test", r"G:\WorkSpace\Doing\CMMI文檔模板.dot")

if __name__ == '__main__':

test()

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394539.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394539.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394539.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

gprof, Valgrind and gperftools - an evaluation of some tools for application level CPU profiling on

2019獨角獸企業重金招聘Python工程師標準>>> In this post I give an overview of my evaluation of three different CPU profiling tools: gperftools, Valgrind and gprof. I evaluated the three tools on usage, functionality, accuracy and runtime overhead…

xp計算機屬性打不開,xp系統我的電腦右鍵屬性打不開怎么辦

在使用xp系統過程中,我們經常需要打開“我的電腦”右鍵屬性,查看系統信息以及進行虛擬內存、性能方面的設置,不過有深度技術ghost xp sp3純凈版用戶右鍵點擊我的電腦,發現右鍵菜單中的“屬性”打不開,出現這個問題通常是注冊表禁用了這個屬性,下面小編跟大家介紹xp系統我的電腦…

狀態機學習(二)解析INI文件

題目來自<系統程序員成長計劃> 作者:李先靜. 狀態變化如下 #include <string> #include <iostream> using namespace std;string s "[GRP]\n\ name def \n\ data 2016.11.29 \r\n\ ; this is a comment \r\n\ str this is a test \n\ [zhangshan]…

接口之用例編寫、驗證

一、用Excel編寫用例&#xff08;xlsx格式&#xff09; 截圖僅供參考&#xff0c;實際用例編寫需要根據實際情況來。 二、用例加載、驗證 1、數據的加載 import xlrd,xlwt #python操作excel主要用到xlrd和xlwt這兩個庫&#xff0c;即xlrd是讀excel&#xff0c;xlwt是寫excel的庫…

計算機二級word真題書娟,計算機二級word試題.docx

PAGEPAGE # / 80Word試題在考生文件夾下打開文檔 word.docx &#xff0c;按照要求完成下列操作并以該文件名( word.docx )保存文檔。某高校為了使學生更好地進行職場定位和職業準備&#xff0c;提高就業能力&#xff0c;該校學工處將于2013 年 4月 29 日(星期五) 19:30-21:30 在…

農場js_通過銷售農場商品來解釋Web API

農場jsby Kevin Kononenko凱文科諾年科(Kevin Kononenko) 通過銷售農場商品來解釋Web API (Web APIs explained by selling goods from your farm) If you have been to a farmer’s market or farm stand, then you can understand the concept of an application programmin…

python安裝pyqt4_windows下安裝PyQt4

第一步&#xff1a;確認自己電腦上的Python版本。然后下載對應的.whl文件下載第二步&#xff1a;https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4上下載對應版本版本的.whl文件。例如cp-27-cp27m就代表是python2.7的版本。如果要下載python3.6且電腦是64位的則需要下載PyQt…

repcached配置與簡單測試

安裝libevent-devel進行configure。安裝在文件夾/usr/local/repcached下編譯安裝完畢啟動11211節點啟動11212節點編寫文件驗證復制&#xff08;分別向1、2節點存入數據&#xff0c;驗證復制&#xff09;ruby執行結果

為Activity設置特定權限才能啟動

1.在AndroidManifest文件中&#xff0c;聲明一個權限&#xff0c;并在activity中添加屬性 <!--聲明權限&#xff0c;權限名一般為包名permission類名 --><permission android:name"com.jikexueyuan.notepad.specpermission.permission.MyAty"/> <acti…

nashPay項目遇到的問題

瀏覽器提示錯誤代碼&#xff1a; Failed to load resource: net::ERR_CONNECTION_REFUSED 出現這個問題是core服務異常&#xff0c;重啟core服務可解決 layUi 下拉框賦值 var loadZhongduan function (data) { admin.req({ url: baseUrl "shoukuanZhongduan/getList&quo…

使用Express在Node.js中實現非常基本的路由

by Pau Pavn通過保羅帕文(PauPavn) 使用Express在Node.js中實現非常基本的路由 (Really, really basic routing in Node.js with Express) The goal of this story is to briefly explain how routing works in Express while building a simple — very simple — Node app.這…

計算機抄作通用模塊,通用命令行模塊的設計及實現

摘要&#xff1a;自從上個世紀八十年代以來,圖形用戶界面得到快速發展,計算機逐漸進入各類企業,家庭,其應用得到廣泛的推廣.對比起命令行界面來說,圖形界面在交互性上有著不可比擬的優勢.但在一些需要執行大量重復性工作的方面,例如在系統管理上,命令行界面提供的腳本功能,能夠…

python讀寫磁盤扇區數據_C++-如何直接讀取Windows磁盤扇區的數據?

1.通過CreateFile系列來完成讀寫扇區可以通過CreateFile打開磁盤邏輯分區&#xff0c;還要通過SetFilePointer以文件操作的方式把指針移到要操作的磁盤扇區開始處&#xff0c;在定位到要訪問的扇區開始位置后就可以通過ReadFile或WriteFile函數實施相應的讀寫訪問了&#xff0c…

公司 郵件 翻譯 培訓 長難句 結課

今天結課啦。。。。。。 明天培訓總結&#xff0c;講翻譯技巧總結。 1new forms of thoughts as well as new subjects for thought must arise in the future as they have in the past, giving rise to new standards of elegance. 2if the small hot spots look as expected…

元祖(轉載)

一.基本數據類型  整數&#xff1a;int  字符串&#xff1a;str(注&#xff1a;\t等于一個tab鍵)  布爾值&#xff1a; bool  列表&#xff1a;list   列表用[]  元祖&#xff1a;tuple  元祖用&#xff08;&#xff09;  字典&#xff1a;dict注&#xff1a;所…

leetcood學習筆記-226- 翻轉二叉樹

題目描述&#xff1a; 第一次提交&#xff1a; class Solution(object):def invertTree(self, root):""":type root: TreeNode:rtype: TreeNode"""if not root:return Nonetemp root.leftroot.left root.rightroot.right temp# root.left,…

現代JavaScript中的精美圖案:制冰廠

I’ve been working with JavaScript on and off since the late nineties. I didn’t really like it at first, but after the introduction of ES2015 (aka ES6), I began to appreciate JavaScript as an outstanding, dynamic programming language with enormous, expres…

惠普omen測試軟件,雙GTX1080奢華魔方PC 惠普OMEN X評測

惠普最近一段時間在游戲PC領域著力發力&#xff0c;桌面的暗影精靈家族熱賣&#xff0c;如火如荼的勢頭終于傳導到了臺式機領域。而今&#xff0c;惠普也終于有了自己正統意義上的重型武器——桌面游戲臺式機OMEN 900暗影精靈II 系列。今天我們就要為大家評測這款三萬元的臺式機…

python 清華鏡像_Anaconda3清華鏡像 V5.3.1 最新免費版

相關軟件軟件大小版本說明下載地址Anaconda3清華鏡像是一款功能強大的python管理工具&#xff0c;此軟件集成了Conda和Python等大量科學計算分析的包&#xff0c;可以幫助用戶快速實現項目環境的配置&#xff0c;有需要的趕快來試試吧&#xff01;【功能特點】1、省時省心&…

Qt第五課 無構造函數可以接受源類型,或構造函數重載決策不明確

場景QJsonArray rgParams { 10, 20, 30, 40 };編譯代碼的時候出錯&#xff0c;C11標準才支持這種類的初始化列表語法&#xff0c;因此如果當前VS的版本過低&#xff0c;必須調整已有的代碼&#xff0c;例子如下&#xff1a;QJsonArray rgParams;rgParams.insert(0, 10);rgPar…