在 Outlook 中使用 Python 发送给多个密件抄送收件人

Using Python to send to multiple BCC recipients in Outlook

我可以使用以下方法在我的 Outlook 电子邮件中获取多个收件人的列表:

for recipient in emails:
     mail.Recipients.Add(recipient)

但我似乎无法弄清楚如何将所有这些收件人放在密件抄送字段中,而不是收件人字段中。

如果我尝试遍历所有 mail.Recipients,例如

mail.Recipients[0].Type = olBCC 

我收到一个错误:

NameError: name 'olBCC' is not defined

我想做的是向密件抄送而不是收件人字段中的每个人发送一封电子邮件,因为我的电子邮件信息对于每封电子邮件都是完全相同的。

完整代码如下:

 import win32com.client
 from openpyxl import load_workbook
 wb = load_workbook(filename = r"C:\file.xlsx")
 sheets = wb.sheetnames

 ws = wb[sheets[0]]
 rows = ws.max_row
 columns = ws.max_column
 count = 0
 emails = []

 for row in range(1,rows):
     if count != 0:
         break
     for column in range(1,columns):
         if '@blah' in str(ws.cell(row,column).value):
            count = count+1
            email_column = column
            
 for email_row in range(row,rows):
     email = str(ws.cell(email_row,email_column).value)
     if  email not in emails and ('None' not in email):
         if abs(int(ws.cell(email_row,6).value)) <5:
             emails.append(email)         

 outlook = win32com.client.Dispatch('outlook.application')
 mail = outlook.CreateItem(0)

 for recipient in emails:
      mail.Recipients.Add(recipient)

 mail.Subject = 'hi'
 email_body = ("Blah. Blah.") 
 mail.Body = email_body
 mail.Display(True)

@gowridev 在评论中写道。

解决方案(为 python3 修改)在这里:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
msg = outlook.OpenSharedItem(r"C:\test_msg.msg")

print(msg.SenderName)
print(msg.SenderEmailAddress)
print(msg.SentOn)
print(msg.To)
print(msg.CC)
print(msg.BCC)
print(msg.Subject)
print(msg.Body)

count_attachments = msg.Attachments.Count
if count_attachments > 0:
    for item in range(count_attachments):
        print msg.Attachments.Item(item + 1).Filename

del outlook, msg

这里是 link: Parsing outlook .msg files with python

olBCC 是 3.

其次,Recipients集合是基于1,而不是0。

第三,Recipients.Addreturns一个Recipient对象。您的代码忽略调用的结果:

for recipient in emails:
     mail.Recipients.Add(recipient).Type = 3