Python郵件發送指令碼(Linux,Windows)通用

來源:互聯網
上載者:User

標籤:指令碼   mil   else   serve   dos   帳號   except   windows   check   

指令碼
#!/usr/bin/python#-*- coding:utf-8 -*-#Python Mail for chenglee#if fileformat=dos, update fileformat=unix#code:set fileformat=unix#check:set ff ?import smtplibfrom email.mime.text import MIMETextfrom email.utils import formataddrmy_sender=‘[email protected]‘    # 寄件者郵箱帳號my_pass = ‘xxxxxxxxxxx‘              # 寄件者郵箱密碼(當時申請smtp給的口令)my_user=‘[email protected]‘      # 收件者郵箱帳號,我這邊發送給自己def mail():    ret=True    try:        msg=MIMEText(‘11填寫郵件內容‘,‘plain‘,‘utf-8‘)        msg[‘From‘]=formataddr(["寄件者暱稱",my_sender])  # 括弧裡的對應寄件者郵箱暱稱、寄件者郵箱帳號        msg[‘To‘]=formataddr(["收件者暱稱",my_user])              # 括弧裡的對應收件者郵箱暱稱、收件者郵箱帳號        msg[‘Subject‘]="11郵件主題-測試"                # 郵件的主題,也可以說是標題        server=smtplib.SMTP_SSL("smtp.qq.com", 465)  # 寄件者郵箱中的SMTP伺服器,連接埠是465        server.login(my_sender, my_pass)  # 括弧中對應的是寄件者郵箱帳號、郵箱密碼        server.sendmail(my_sender,[my_user,],msg.as_string())  # 括弧中對應的是寄件者郵箱帳號、收件者郵箱帳號、發送郵件        server.quit()# 關閉串連    except Exception:# 如果 try 中的語句沒有執行,則會執行下面的 ret=False        ret=False    return retret=mail()if ret:    print("郵件發送成功")else:    print("郵件發送失敗")  

在windows下用的時候,前兩行可去可留!

測試結果

windows

linux

提示

 作為發送人, 需要開啟兩項服務。

 

 

 

 

Python郵件發送指令碼(Linux,Windows)通用

聯繫我們

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