2

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.

4
  • Remove the try/except and see if you are getting an error. If you are, post the exception in the question. Jul 14, 2014 at 20:05
  • I just did. Thank you. Jul 14, 2014 at 20:15
  • If you add a line print "attachment exists:", os.path.exists(attachment1) does it print True? What if you try to attach a plain text file?
    – Oliver
    Jul 14, 2014 at 22:07
  • It doesn't print anything when entering that line. And it gives the same error when attaching a plain text file. Jul 15, 2014 at 15:32

2 Answers 2

1

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:

1
  • Thank you. I tried as an administrator and it gives me another error that the Server Execution Failed. I also tried the Extended MAPI as stated in the link you provided. And still get the error: (-2147024809, 'The parameter is incorrect.', None, None) Jul 15, 2014 at 15:35
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.