python寄送電子郵件模組smtplib

來源:互聯網
上載者:User

標籤:smtplib

一.簡介:

   電子郵件是最流行的互連網應用之一,在系統管理中,經常需要使用郵件來警示資訊,業務品質報告等。方便營運人員在第一時間瞭解業務的服務狀態,將通過使用python的smtplib模組來實現郵件的發送功能,能類比一個smtp的用戶端,通過與smtp伺服器互動來實現郵件的發送功能,可以理解成foxmail的發郵件功能,在第一次使用的時候需要誒只smtp的主機地址,郵箱帳號密碼等資訊。python 2.4以上的版本預設就內建了smtplib模組,無需額外安裝。

二.smtplib模組的常用類與方法

    smtp類定義:smtp.SMTP([host[,port[,local_hostname[,timeout]]]]),作為smtp的建構函式,功能是與smtp伺服器建立串連,成功之後,就可以向伺服器發送相關請求,如:登入,校正,發送,推出等。host參數為遠程smtp主機地址,比如:smtp.163.com,port 25,local_hostname的作用是在本地主機的FQDN發送HELO/EHLO(表示使用者身份)指令,timeout為串連或者嘗試的逾時時間。

#!/usr/bin/python #coding: utf-8#mail:[email protected]#htttp://www.mrliangqi.comimport  smtplib import  stringHOST = "smtp.126.com"SUBJECT = "HI,test email from  python!" TO  = "[email protected]"FROM = "[email protected]"TEXT = "hello  world"BODY = string.join((                "From: %s" %FROM,                "TO: %s" % TO,                "Subject: %s" %  SUBJECT,                "",                TEXT                ),"\r\n")try:        server = smtplib.SMTP()        server.connect(HOST,"25")   #串連遠程主機smtp主機的方法        server.starttls()        server.login("[email protected]","mailpwd")  #遠程smtp主機方法。引號中是帳號和密碼        server.sendmail(FROM,[TO],BODY)        server.quit() #斷開smtp伺服器        print "Mail sent successfully"except exception, e:        print "Mail sendfail!"


本文出自 “Internet&Linux” 部落格,轉載請與作者聯絡!

python寄送電子郵件模組smtplib

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.