Error Using Python to Send Automail Outlook

Viewed 30

I want to send auto mail from outlook using python script. but I'm getting below error.

below is the code:

import win32com.client
import csv

import csv
with open('D:/Vishal Somal/Python/people.txt', 'r') as file:
    reader = csv.reader(file)
    distro = [row for row in reader]
    #print(distro)
    #for row in reader:
        #print(row)

chunks = [distro[x:+100] for x  in range (0, len(distro), 100)]
outlook = win32com.client.Dispatch('outlook.application')


for chunk in chunks:
    for email in chunk:
        message = outlook.CreateItem(0)
        message.To = email
        print(message.To)
        message.Subject= "Test mail"
        message.Body = "if you received attachment then please reply"
        attachment1 = "D:/Vishal Somal/Python/Autobo/output/ANT_Stock.xlsx"
        attachment2 = "D:/Vishal Somal/Python/Autobo/output/ANT_Transit.xlsx"
        message.Attachments.Add(attachment1)
        message.Attachments.Add(attachment2)
        message.Send()

error:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "C:\Users\vishals\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 686, in __setattr__
    self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The object does not support this method.', None, 0, -2147352567), None)
0 Answers
Related