A
A
Alan Gibizov2021-12-07 16:41:54
Python
Alan Gibizov, 2021-12-07 16:41:54

How to get (resolve) a group of contacts from Outlook when sending messages via MAPI?

I'm trying to send an email to an Outlook 2016 contact group named "test1" from a python3 script:

from win32com.client.gencache import EnsureDispatch
from win32com.client import constants

outlook = EnsureDispatch("Outlook.Application")
newMail = outlook.CreateItem(constants.olMailItem)
newMail.Subject = "This is subject"
samplegrp = newMail.Recipients.Add("test1")
samplegrp.Type = constants.olTo
newMail.Recipients.ResolveAll()
newMail.Send()

The resolver returns "False" and the Send() method complains about the lack of addresses.

If we put an existing address in the address list into newMail.To, it "resolves" (newMail.Recipients.ResolveAll() returns "True") and the message is sent successfully.

My attempts to get to this contact group object of type AddressEntry "test1" via the API seem to be successful:
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants

outlook = EnsureDispatch("Outlook.Application")
newMail = outlook.CreateItem(constants.olMailItem)
namespace = outlook.GetNamespace("MAPI")
addresslist = namespace.AddressLists

addressentries = addresslist.Item(1).AddressEntries
samplegrp = [item for item in addressentries if item.Name == 'test1'][0]
print(samplegrp.Name)

returns "test1", but neither "resolve" it, nor stupidly "put" it into newMail.To and send it to it I failed. Among the methods and attributes of this object, I did not find anything that would help me to somehow pick out the contents of the "test1" group from outlook.

After...
Я понимаю, что можно просто поместить в атрибут .To нужные адреса множеством разных способов (передать строку с адресами через ";" или разными другими методами), но мне хотелось бы задействовать именно группу контактов из Outlook.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-12-07
@phaggi

Thanks to a hint from Viktor T2 , I finally got to the mailing lists (type DistListItem) and found the methods and fields of this class in the MS documentation. Hooray!

the code
import win32com.client as win32
from win32com.client.gencache import EnsureDispatch
 
 
 
 
def get_maillist(maillistname: str):
    """
 
    :param maillistname: str name of Group contacts at Outlook
    :return: string of addresses (del = ;) of Group contacts
    (empty if Group 'maillistname' not found)
    """
    outlook = EnsureDispatch("Outlook.Application")
    olNamespace = outlook.GetNamespace("MAPI")
    olFolder = olNamespace.GetDefaultFolder(10)
    olConItems = olFolder.Items
    mail_list = []
    for olItem in olConItems:
        if "_DistListItem" in str(type(olItem)) and olItem.DLName == maillistname:
            counter = olItem.MemberCount
            while bool(counter):
                mail_list.append(olItem.GetMember(counter).Address)
                counter -= 1
    if bool(mail_list):
        result = ';'.join(mail_list)
    else:
        result = ''
    return result
 
if __name__ == '__main__':
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = get_maillist('test1')
    mail.Subject = 'testmail'
    print(mail.Recipients.ResolveAll())
    print(mail.To)
    mail.Send()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question