Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mapisend.py 3.5KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. """module to send mail with Extended MAPI using the pywin32 mapi wrappers..."""
  3. # this was based on Jason Hattingh's C++ code at http://www.codeproject.com/internet/mapadmin.asp
  4. # written by David Fraser <davidf at sjsoft.com> and Stephen Emslie <stephene at sjsoft.com>
  5. # you can test this by changing the variables at the bottom and running from the command line
  6. from win32com.mapi import mapi, mapitags
  7. def SendEMAPIMail(
  8. Subject="", Message="", SendTo=None, SendCC=None, SendBCC=None, MAPIProfile=None
  9. ):
  10. """Sends an email to the recipient using the extended MAPI interface
  11. Subject and Message are strings
  12. Send{To,CC,BCC} are comma-separated address lists
  13. MAPIProfile is the name of the MAPI profile"""
  14. # initialize and log on
  15. mapi.MAPIInitialize(None)
  16. session = mapi.MAPILogonEx(
  17. 0, MAPIProfile, None, mapi.MAPI_EXTENDED | mapi.MAPI_USE_DEFAULT
  18. )
  19. messagestorestable = session.GetMsgStoresTable(0)
  20. messagestorestable.SetColumns(
  21. (mapitags.PR_ENTRYID, mapitags.PR_DISPLAY_NAME_A, mapitags.PR_DEFAULT_STORE), 0
  22. )
  23. while True:
  24. rows = messagestorestable.QueryRows(1, 0)
  25. # if this is the last row then stop
  26. if len(rows) != 1:
  27. break
  28. row = rows[0]
  29. # if this is the default store then stop
  30. if (mapitags.PR_DEFAULT_STORE, True) in row:
  31. break
  32. # unpack the row and open the message store
  33. (eid_tag, eid), (name_tag, name), (def_store_tag, def_store) = row
  34. msgstore = session.OpenMsgStore(
  35. 0, eid, None, mapi.MDB_NO_DIALOG | mapi.MAPI_BEST_ACCESS
  36. )
  37. # get the outbox
  38. hr, props = msgstore.GetProps((mapitags.PR_IPM_OUTBOX_ENTRYID), 0)
  39. (tag, eid) = props[0]
  40. # check for errors
  41. if mapitags.PROP_TYPE(tag) == mapitags.PT_ERROR:
  42. raise TypeError("got PT_ERROR instead of PT_BINARY: %s" % eid)
  43. outboxfolder = msgstore.OpenEntry(eid, None, mapi.MAPI_BEST_ACCESS)
  44. # create the message and the addrlist
  45. message = outboxfolder.CreateMessage(None, 0)
  46. # note: you can use the resolveaddress functions for this. but you may get headaches
  47. pal = []
  48. def makeentry(recipient, recipienttype):
  49. return (
  50. (mapitags.PR_RECIPIENT_TYPE, recipienttype),
  51. (mapitags.PR_SEND_RICH_INFO, False),
  52. (mapitags.PR_DISPLAY_TYPE, 0),
  53. (mapitags.PR_OBJECT_TYPE, 6),
  54. (mapitags.PR_EMAIL_ADDRESS_A, recipient),
  55. (mapitags.PR_ADDRTYPE_A, "SMTP"),
  56. (mapitags.PR_DISPLAY_NAME_A, recipient),
  57. )
  58. if SendTo:
  59. pal.extend(
  60. [makeentry(recipient, mapi.MAPI_TO) for recipient in SendTo.split(",")]
  61. )
  62. if SendCC:
  63. pal.extend(
  64. [makeentry(recipient, mapi.MAPI_CC) for recipient in SendCC.split(",")]
  65. )
  66. if SendBCC:
  67. pal.extend(
  68. [makeentry(recipient, mapi.MAPI_BCC) for recipient in SendBCC.split(",")]
  69. )
  70. # add the resolved recipients to the message
  71. message.ModifyRecipients(mapi.MODRECIP_ADD, pal)
  72. message.SetProps([(mapitags.PR_BODY_A, Message), (mapitags.PR_SUBJECT_A, Subject)])
  73. # save changes and submit
  74. outboxfolder.SaveChanges(0)
  75. message.SubmitMessage(0)
  76. if __name__ == "__main__":
  77. MAPIProfile = ""
  78. # Change this to a valid email address to test
  79. SendTo = "an.invalid at address"
  80. SendMessage = "testing one two three"
  81. SendSubject = "Testing Extended MAPI!!"
  82. SendEMAPIMail(SendSubject, SendMessage, SendTo, MAPIProfile=MAPIProfile)