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


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

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

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

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

  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對象創建連接

或者:

 

 

 

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

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

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

 

 最終及解決代碼如下:

#!/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()

  

 如果你對上面問題還有疑問可以加群咨詢我:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM