2

For my machine, after logging in the windows system, the outlook logon automatically with Anonymous Authentication.

The outlook connects to Microsoft Exchange using HTTP, and connects using SSL only.

The Microsoft Exchange server is

code@ms.exchange.server

The principal name in the certificate is known when connecting to proxy servers.

Header:email.server.sample

The URL to connect to my proxy server for Exchange is known.

https://email.server.sample

"Negotiate Authentication" is used when connecting to the proxy server for Exchange.

Now, the question is, how can python sends out the email with this kind of situation?

Warmer
  • 107
  • 1
  • 7
  • exchangelib does not have support for native Windows SSO yet. Proxy support isdocumented somewhat at https://github.com/ecederstrand/exchangelib#proxies-and-custom-tls-validation – Erik Cederstrand Jan 23 '19 at 07:06

1 Answers1

0

Here is what I made for this situation hope works for you too:

refer to this page for documentation:

Documentation for outlook object

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
Aiden Zhao
  • 633
  • 4
  • 15
  • I get this error for the mail.send()------------------pywintypes.com_error: (-2147467260, 'Operation aborted', None, None) – Warmer Mar 06 '19 at 07:31
  • https://stackoverflow.com/questions/26807942/sending-an-email-through-python here someone with the same issue said some security reason in outlook is preventing it. – Aiden Zhao Mar 07 '19 at 08:02