我有一个脚本循环遍历充满电子邮件地址的Excel工作表。它将电子邮件放在模板电子邮件的“收件人”字段中,然后将其保存在文件夹中。我的想法是稍后在手动按下SEND按钮之前查看电子邮件。
问题出在发送后,我收到一个退回错误:“您的电子邮件帐户都无法发送给此收件人。”对于TO和CC。如果我手动重新输入TO字段中的SAME电子邮件地址,则会发送正常邮件。所以收件人对象或其他东西出了问题。以下是提取的相关代码:
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('OESP.agencysupport@icfi.com')
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)
无论如何,经过一些(大量)在线搜索,我发现Outlook可能没有将我的TO和CC地址视为SMTP项目......还没有弄清楚如何确认这一点。有什么想法吗?
答案 0 :(得分:0)
添加以下内容:mail.Recipients.ResolveAll()
应该可以解决问题。
在这里回答答案: https://stackoverflow.com/a/21011296/8350440
此处的Microsoft文档:https://docs.microsoft.com/en-us/office/vba/api/outlook.recipients.resolveall