python通过MX端点发送邮件

问题描述

我正在尝试使用 python 发送电子邮件。当我通过我自己的 Outlook 配置文件发送它时效果很好。但我需要从共享邮件地址(没有密码和 Outlook 配置文件)并通过 Microsoft Exchange(Microsoft Office 365 SMTP 中继)创建的 MX 端点发送这些电子邮件

因此,我应该在某处提供发件人邮件“info@entreprise.com”和一个 MX 端点“society-com.mail.protection.outlook.com”。我知道我需要使用端口 25。如果我尝试将此端点用作 SMTP 服务器,例如 server = smtplib.SMTP("society-com.mail.protection.outlook.com",25) 不起作用 并直接使用 Outlook MAPI 接口,如下所示,它可以要求我更改配置文件,但信息邮件没有 Outlook 配置文件。我迷路了,不知道如何通过这个端点发送邮件

此外,我在办公室和通过 VPN 尝试了这两种方法。但是……身体在域中无济于事。 谢谢你的帮助。这是我的代码(取自其他人),它通过 MAPI 从我的电子邮件地址运行得很好:

    def SendEMAPIMail(Subject="",Message="",SendTo=None,SendCC=None,SendBCC=None,MAPIProfile=None):
        """Sends an email to the recipient using the extended MAPI interface
        Subject and Message are strings
        Send{To,CC,BCC} are comma-separated address lists
        MAPIProfile is the name of the MAPI profile"""
    
        # initialize and log on
        mapi.MAPIInitialize(None)
        session = mapi.MAPIlogonEx(0,MAPIProfile,None,mapi.MAPI_EXTENDED | mapi.MAPI_logoN_UI | mapi.MAPI_NEW_SESSION)
        #session = mapi.MAPIlogonEx(0,mapi.MAPI_EXTENDED | mapi.MAPI_logoN_UI)
        messagestorestable = session.GetMsgStoresTable(0)
        messagestorestable.SetColumns((mapitags.PR_ENTRYID,mapitags.PR_disPLAY_NAME_A,mapitags.PR_DEFAULT_STORE),0)
    
        while True:
            rows = messagestorestable.QueryRows(1,0)
            # if this is the last row then stop
            if len(rows) != 1:
                break
            row = rows[0]
            # if this is the default store then stop
            if ((mapitags.PR_DEFAULT_STORE,True) in row):
                break
    
        # unpack the row and open the message store
        (eid_tag,eid),(name_tag,name),(def_store_tag,def_store) = row
        msgstore = session.OpenMsgStore(0,eid,mapi.MDB_NO_DIALOG | mapi.MAPI_BEST_ACCESS)
    
        # get the outBox
        hr,props = msgstore.GetProps((mapitags.PR_IPM_OUTBox_ENTRYID),0)
        (tag,eid) = props[0]
        # check for errors
        if mapitags.PROP_TYPE(tag) == mapitags.PT_ERROR:
            raise TypeError('got PT_ERROR instead of PT_BINARY: %s' % eid)
        outBoxfolder = msgstore.OpenEntry(eid,mapi.MAPI_BEST_ACCESS)
    
        # create the message and the addrlist
        message = outBoxfolder.CreateMessage(None,0)
        # note: you can use the resolveaddress functions for this. but you may get headaches
        pal = []
    
        def makeentry(recipient,recipienttype):
            return ((mapitags.PR_RECIPIENT_TYPE,recipienttype),(mapitags.PR_SEND_RICH_INFO,False),(mapitags.PR_disPLAY_TYPE,0),(mapitags.PR_OBJECT_TYPE,6),(mapitags.PR_EMAIL_ADDRESS_A,recipient),(mapitags.PR_ADDRTYPE_A,'SMTP'),(mapitags.PR_disPLAY_NAME_A,recipient))
    
        if SendTo:
            pal.extend([makeentry(recipient,mapi.MAPI_TO) for recipient in SendTo.split(",")])
        if SendCC:
            pal.extend([makeentry(recipient,mapi.MAPI_CC) for recipient in SendCC.split(",")])
        if SendBCC:
            pal.extend([makeentry(recipient,mapi.MAPI_BCC) for recipient in SendBCC.split(",")])
    
        # add the resolved recipients to the message
        message.ModifyRecipients(mapi.MODRECIP_ADD,pal)
        message.SetProps([(mapitags.PR_BODY_A,Message),(mapitags.PR_SUBJECT_A,Subject)])
    
        # save changes and submit
        outBoxfolder.SaveChanges(0)
        message.SubmitMessage(0)
    
    
    if __name__ == '__main__':
        MAPIProfile = ""
        # Change this to a valid email address to test
        SendTo = receiver_email
        SendMessage = mail_body
        SendSubject = mail_subject
        SendEMAPIMail(SendSubject,SendMessage,SendTo,MAPIProfile=MAPIProfile)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)