0

I have a list of IDs that I need to turn into emails, but all of the addresses are the same, but all the IDs are unique. If I was in outlook I could just press ctrl K and have the address autocompleted. Is there a way to have similar functionality in python if the address path isn't readily available? This is what I currently have in a loop. It works if I add the @PATH.com, but not all the addresses are "@PATH.com"....

import win32com.client as win32



outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = U_ID  #This is what the address path need to be completed
mail.Subject = 'auto email test'
mail.Body = "body"
mail.send
jvvon
  • 29
  • 4

1 Answers1

0

You need to use the Recipients property of the MailItem class which returns a Recipients collection that represents all the recipients for the Outlook item. The Add method creates a new recipient in the Recipients collection. A recipient can be specified by a string representing the recipient's display name, alias, or full SMTP email address. But in this case, you can use the Resolve method which attempts to resolve a Recipient object against the Address Book. For example, a sample VBA script look like this:

Sub AssignTask() 
 Dim myItem As Outlook.TaskItem 
 Dim myDelegate As Outlook.Recipient 

 Set MyItem = Application.CreateItem(olTaskItem) 

 MyItem.Assign 

 Set myDelegate = MyItem.Recipients.Add("Eugene Astafiev") 

 myDelegate.Resolve 

 If myDelegate.Resolved Then 
    myItem.Subject = "Prepare Agenda For Meeting" 
    myItem.DueDate = Now + 30 
    myItem.Display 
    myItem.Send 
 End If

End Sub 
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I'm sorry, I'm not following you at all. Is this Python? I'm trying to incorporate your suggestions, but on "sub", python doesn't recognize it. Do I need to import a different module? @Eugene-Astafiev – jvvon Jun 03 '20 at 15:10
  • Sorry, I am not familiar with Python syntax. It is a sample VBA sub which shows how you can use properties and methods from the Outlook object model to get the job done. OOM is common for all kind of applications, so property and method names are the same for all kind of programming languages. – Eugene Astafiev Jun 03 '20 at 15:25