Answer the question
In order to leave comments, you need to log in
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()
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)
Answer the question
In order to leave comments, you need to log in
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!
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 questionAsk a Question
731 491 924 answers to any question