Alvaro Morales Solis
Alvaro Morales Solis

Reputation: 239

win32com not sending attachments

This is my first question, so I hope it is ok.

I am trying to send an email with an attachement with Python using win32com with Outlook 2010. And it sends the email. But it is not sending the attachment. I based my code in an example given here.

def email_tamplate(*args):
  Format = { 'UNSPECIFIED' : 0, 'PLAIN' : 1, 'HTML' : 2, 'RTF'  : 3}
  profile = "Outlook"
  #session = win32com.client.Dispatch("Mapi.Session")
  outlook = win32com.client.Dispatch("Outlook.Application")
  #session.Logon(profile)
  mainMsg = outlook.CreateItem(0)
  mainMsg.To = str(login_entry.get())+"@amazon.com"
  mainMsg.Subject = "Sauron personal report request between "+str(start_date.get())+" and "+str(end_date.get())
  mainMsg.BodyFormat = Format['RTF']
  mainMsg.HTMLBody = body

  try:
    attachment1= ('C:\\Users\\' + str(login_entry.get()) + '\\My Documents\\status_email.csv')
    mainMsg.Attachments.Add(attachment1)
  except:
    pass                                   

  mainMsg.Send()

Here's the error message I get when not using the Try/except:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Python33\lib\tkinter__init__.py", line 1475, in call return self.func(*args) File "C:\Users\alvaros\Dev\Sauron\v2\Sauron v2.2.1.py", line 478, in status_email email_tamplate() File "C:\Users\alvaros\Dev\Sauron\v2\Sauron v2.2.1.py", line 424, in email_tamplate mainMsg.Attachments.Add(attachment1) File ">", line 3, in Add pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "You don't have appropriate permission to perform this operation.", None, 0, -2147024891), None)

Everything works except that the attachment is not sent. The path of the attachment is correct and the file is there.

Thank you.

Upvotes: 2

Views: 9589

Answers (2)

Alvaro Morales Solis
Alvaro Morales Solis

Reputation: 239

This was resolved already. Thank you for the help. It might have been an indent issue. Because it basically got solved by itself.

Upvotes: 0

Mike Driscoll
Mike Driscoll

Reputation: 33101

You may have to run the program as Administrator or if that doesn't work, then[enter link description here][1] you may need to use "Extended MAPI" to get this to work:

On the other hand, this thread talked about editing the properties of the PST file itself or editing its properties in the Windows registry:

Upvotes: 1

Related Questions