smtp outlook郵件發送非受權碼模式

1.原由:send fail SMTP AUTH extension not supported by server. 使用端口25 和587均失效出現此問題html

首先前往outlook修改設置pop和IMAP開啓,容許第三方調用:https://outlook.live.com/owa/?path=/options/popandimappython

【在使用QQ郵箱IOS端的APP添加outlook郵箱時,顯示IMAP沒有開啓】是否意指當您在iOS手機設備中進行配置微軟賬戶至第三方域名郵箱(QQ郵箱)時,git

系統提示Outlook郵箱中的IMAP並未開啓?github

  1. 請您前往網頁版Outlook郵箱的【POP和IMAP】;
  2. 接着,請您在【POP選項】選擇【是】;
  3. 在【使用POP的設備和應用能夠設置爲刪除重Outlook下載的郵件】中,咱們建議您點選【不容許設備和應用刪除來自Outlook的郵件。它會將郵件移動到特殊的POP文件夾】,這是爲了防止您的郵件丟失的狀況;
  4. 請完成以上步驟後,單擊【保存】便可。

 

查詢緣由:版本使用python3.7,官網作出以下變更:server_hostname cannot be an empty string or start with a leading dot.服務器

也就是說如今對於3.7須要對非ssl模式傳入smtp server對象傳入str模式的host地址再進行server對象建立鏈接app

或者:eclipse

 

 

 

在github有人曾給出以下修復針對ssl,非sslui

https://github.com/tp4a/teleport/commit/dea9c48d825e7bac5bbc41006bc993713e4b516fspa

嘗試修復:服務器不支持SMTP AUTH擴展。3d

 

 最終及解決代碼以下:

#!/usr/bin/python3
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.image import MIMEImage

def main():

    sender='chen1054@outlook.com'
    receiverList=['chen1054@outlook.com']
    user='chen1054@outlook.com'
    emailPwd='youroutlookpwd'#用戶郵箱密碼無需開通受權碼
    smtpServer=r'smtp-mail.outlook.com'
    commonPort=587 # 25也有效
    emailTitle='Hello,World!'
    htmlPath=r'F:/eclipse/readme/readme_eclipse.html'
    attachPathList=[r'C:\Users\Administrator\PycharmProjects\Supro\src\logs\Info\20190330.log',r'C:\Users\Administrator\PycharmProjects\Supro\src\logs\Info\testpc.png']
    emailChanel(sender,receiverList,user,emailPwd,smtpServer,commonPort,emailTitle,htmlPath,attachPathList)


def emailChanel(sender,receiverList,user,emailPwd,smtpServer,commonPort,emailTitle,htmlPath=None,attachPathList=None):
    multiPart=MIMEMultipart()
    multiPart['From']=sender
    multiPart['To']=','.join(receiverList)
    subject=emailTitle
    multiPart['Subject']=Header(subject,"utf-8")
    if os.path.isfile(htmlPath):
        if os.path.exists(htmlPath):
            pass
        else:
            raise IOError("htmlPath not exist")
    else:
        raise IOError("html path is not file..")
    emailBody=MIMEText(_text=open(htmlPath,'rb').read(),_subtype='html',_charset="utf-8")
    multiPart.attach(emailBody)
    if isinstance(attachPathList,list):
            for attachPath in attachPathList:
                if os.path.exists(attachPath):
                    pass
                else:
                    raise  IOError("attachPath not exist")
    else:
        raise TypeError("expected type is list,but get {}".format(type(attachPathList).__name__))
    for attachPath in attachPathList:
        if os.path.splitext(attachPath)[-1]==".log":
            attach=MIMEText(open(attachPath, 'rb').read(), 'base64', 'utf-8')
            attach["Content-Type"] = 'application/octet-stream'
            attach["Content-Disposition"] = 'attachment; filename="dailyLog.log"' # filename not strict
            multiPart.attach(attach)
        if os.path.splitext(attachPath)[-1]==".png":
            fp = open(attachPath, 'rb')
            msgImage = MIMEImage(fp.read(),_subtype='octet-stream')
            fp.close()
            msgImage.add_header('Content-Disposition', 'attachment', filename="attach.png")
            multiPart.attach(msgImage)
    # https://github.com/tp4a/teleport/commit/dea9c48d825e7bac5bbc41006bc993713e4b516f
    smtp=smtplib.SMTP("{}:{}".format(smtpServer,commonPort))
    try:
        smtp.ehlo()
        smtp.starttls()
        smtp.login(user,emailPwd)
        smtp.sendmail(sender,receiverList,multiPart.as_string())
    except smtplib.SMTPException as e:
        print("send fail",e)
    else:
        print("success")
    finally:
        try:
            smtp.quit()
        except smtplib.SMTPException:
            print("quit fail")
        else:
            print("quit success")
if __name__ == '__main__':
    main()

  

相關文章
相關標籤/搜索