2

对于我的机器,登录windows系统后,outlook自动使用匿名认证登录。

Outlook 使用 HTTP 连接到 Microsoft Exchange,并且仅使用 SSL 连接。

Microsoft Exchange 服务器是

code@ms.exchange.server

连接到代理服务器时,证书中的主体名称是已知的。

Header:email.server.sample

连接到我的 Exchange 代理服务器的 URL 是已知的。

https://email.server.sample

连接到 Exchange 代理服务器时使用“协商身份验证”。

现在,问题是,python 怎么能在这种情况下发送电子邮件呢?

4

1 回答 1

0

这是我为这种情况所做的,希望对你也有用:

有关文档,请参阅此页面:

Outlook 对象的文档

import numpy as np
import pandas as pd
import re
import datetime
from dateutil.relativedelta import relativedelta
import win32com.client as win32
import tldextract
outlook = win32.Dispatch('outlook.application')
import win32com.client 
from win32com.mapi import mapitags
# outlook = win32com.client.Dispatch("Outlook")
outlook_app = win32com.client.Dispatch("Outlook.Application")
MAPI = outlook_app.GetNamespace("MAPI")

inbox = MAPI.GetDefaultFolder(6)

def send_email(To,cc_list = "",
             bcc_list = "", subject = "", htmlbody = ""):
    mail = outlook.CreateItem(0)
    mail.To = To
    mail.cc = cc_list
    mail.Bcc = bcc_list
    mail.Subject = subject
    mail.HTMLBody = htmlbody 
    mail.Send()

def move_and_save_attachments_for_all_inbox_emails():
    done = False
    while(not done):
        counter = 0
        for mail in inbox.Items:
            if(mail.Class == 43):
                counter += 1
                save_current_email_attachments(mail)
                move_current_email(mail)
        if counter == 0:
            done = True

def move_current_email(mail):
    if(mail.Class==43):
        if(mail.SenderEmailType=='EX'):
            sender = mail.Sender.GetExchangeUser().PrimarySmtpAddress
        else:
            sender = mail.SenderEmailAddress
        domainname = tldextract.extract(sender).domain
        try:
            mail.Move(inbox.Folders[domainname.upper()])
        except:
            inbox.Folders.add(domainname.upper())
            mail.Move(inbox.Folders[domainname.upper()])

def save_current_email_attachments(mail):
    if(mail.Class==43):
        if(mail.SenderEmailType=='EX'):
            sender = mail.Sender.GetExchangeUser().PrimarySmtpAddress
        else:
            sender = mail.SenderEmailAddress
        username, domainname = sender.split('@')
        for attachment in mail.Attachments:
            attachment.SaveAsFile('attachments\\'+domainname+'_'+attachment.Filename)

def move_all_mails_into_inbox():
    done = False
    while(not done):
        counter = 0
        for folder in inbox.folders:
            for mail in folder.Items:
                counter += 1
                mail.Move(inbox)
        if(counter == 0):
            done = True
于 2019-01-25T21:23:17.223 回答