Reputation: 215
I have a script that loops through an excel sheet full of email addresses. It puts the email in the TO field of a template email then saves it in a folder. The idea is to later view the email before manually pressing the SEND button.
The problem is after sending, I receive a bounceback error: "None of your e-mail accounts could send to this recipient." for both TO and CC. If I manually re-enter the SAME email address in the TO field it sends fine. So something is wrong with the recipient object or something. Here is the relevant code extracted:
import win32com.client as win32
#This next set of code will create a folder in the OESP.AgencySupport shared mailbox
ol = win32.Dispatch("Outlook.Application")
mapi = ol.GetNamespace('MAPI')
myrecipient = mapi.CreateRecipient('[email protected]')
myrecipient.resolve
if myrecipient.resolved == True:
outlookfolder = mapi.GetSharedDefaultFolder(myrecipient,6).Folders.Add(the_date + 'Invoices')
#This part takes a template file, adds the email, subject, cc and attaches a PDF. This function is called later on in the script.
def emailattachment(pdf,address,date,agency,outlookfolder):
mail = ol.CreateItemFromTemplate(working_dir + r"invoice.oft",outlookfolder)
mail.To = address
mail.SentOnBehalfofName = myrecipient
mail.cc = myrecipient
mail.Subject = str(date) + 'invoice for ' + str(agency)
mail.Attachments.Add(pdf)
#mail.display(True)
mail.Move(outlookfolder)
Anyway after some (lots) of online searching I've discovered that Outlook might not be viewing my TO and CC addresses as SMTP items... have yet to figure out how to confirm this. Any thoughts?
Upvotes: 1
Views: 924
Reputation: 881
Adding this: mail.Recipients.ResolveAll()
should do the trick.
Came across the answer here: https://stackoverflow.com/a/21011296/8350440
Microsoft documention here: https://learn.microsoft.com/en-us/office/vba/api/outlook.recipients.resolveall
Upvotes: 2