Python 學習筆記 發送郵件__Python

來源:互聯網
上載者:User

發送郵件前需要在檔案頭引入

 

from sgmllib import SGMLParser
import smtplib
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
import datetime

 

 

#定義發送郵件的函數
def Send_Mail (Message):
    TODAY = datetime.date.today()
    CURRENTDAY=TODAY.strftime('%Y-%m-%d')
    msg = MIMEMultipart()
    '''
    att = MIMEText(open(r'D:\report\aa.xlsx', 'rb').read(), 'base64', 'gb2312') #設定附件的目錄
    att['content-type'] = 'application/octet-stream'
    att['content-disposition'] = 'attachment;filename="IMD_EBM.xlsx"' #設定附件的名稱
    msg.attach(att)
    '''
    content = str(Message) #本文內容
    body = MIMEText(content,'plain','GBK') #設定字元編碼
    msg.attach(body)

    msgto = ['123@126.com'] # 收件者地址 多個連絡人,格式['aa@163.com'; 'bb@163.com']
    msgfrom = '321@126.com' # 寄件地址 ,
    msg['subject'] = 'Finish at ' CURRENTDAY  #主題
    msg['date']=time.ctime() #時間
    #msg['Cc']='bb@junbao.net' #抄送人地址 多個地址不起作用


    mailuser = '321'  # 使用者名稱
    mailpwd = '321' #密碼

    try:
        smtp = smtplib.SMTP()
        smtp.connect(r'smtp.126.com')# smtp設定
        smtp.login(mailuser, mailpwd) #登入
        smtp.sendmail(msgfrom, msgto, msg.as_string()) #發送
        smtp.close()
        print "success mail"
    except Exception, e:
        print e

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.