向多个用户发送电子邮件时出现异常 - win32com - Python

时间:2018-02-13 05:54:33

标签: python outlook

这是我的代码:

import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
email_t4=a@xyz.com;b@xyz.com;c@xyz.com
mail.To='email_t4'
mail.CC='r@xyz.com'
mail.BCC='s@xyz.com'

mail.Subject='ABC'
mail.Body='XYZ'

mail.HTMLBody=<h1>Report</h1>

attachment4=r'C:\Users\r7\Downloads\frame4.html'

mail.Attachments.Add(attachment4)

mail.Send()

以下是异常消息:

 com_error  Traceback (most recent call
 last) <ipython-input-26-ce3c12b7103e> in <module>()
      30 mail.Attachments.Add(attachment4)
      31 
 ---> 32 mail.Send()  

 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\win32com\client\dynamic.py
 in Send(self)

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 
'Outlook does not recognize one or more names. ', None, 0,
 -2147467259), None)

请在这里帮助我。

1 个答案:

答案 0 :(得分:0)

我已更新您的代码以使其正常工作。错误是由错误的引号引起的。我已经对已编辑的行添加了注释。

import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
email_t4='a@xyz.com;b@xyz.com;c@xyz.com' # Add quotes to value
mail.To=email_t4 # Remove quotes for teating as variable
mail.CC='r@xyz.com'
mail.BCC='s@xyz.com'

mail.Subject='ABC'
mail.Body='XYZ'

mail.HTMLBody='<h1>Report</h1>' # Quoted this as it was not valid syntax

attachment4=r'C:\temp\python\test.txt'

mail.Attachments.Add(attachment4)

mail.Send()