Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception raised while sending email to multiple users - win32com - Python

Tags:

python

outlook

Here is my code:

import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
[email protected];[email protected];[email protected]
mail.To='email_t4'
mail.CC='[email protected]'
mail.BCC='[email protected]'

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()

Here is exception message:

 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)

Please help me here.

like image 997
Ravikant Avatar asked Dec 17 '25 13:12

Ravikant


1 Answers

I have updated your code to make it work. The errors were due to wrong quotes. I have added comments to the lines which are edited.

import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
email_t4='[email protected];[email protected];[email protected]' # Add quotes to value
mail.To=email_t4 # Remove quotes for teating as variable
mail.CC='[email protected]'
mail.BCC='[email protected]'

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()
like image 95
skjoshi Avatar answered Dec 20 '25 16:12

skjoshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!