问题
I want to make an script in python that reads mails from Lotus Notes 8.5 and then create for each email an issue in jira but it returns me this error when I try to read the mails from Lotus:
Traceback (most recent call last):
File "from_Lotus_TO_Jira.py", line 46, in <module>
main()
File "from_Lotus_TO_Jira.py", line 39, in main
folder = notesDatabase.GetView('$Inbox')
File "C:\Python27\lib\site-packages\win32com\gen_py\29131520-2EED-1069-BF5D-00
DD011186B7x0x1x2.py", line 1849, in GetView
ret = self._oleobj_.InvokeTypes(1610743866, LCID, 1, (9, 0), ((8, 1),),pName
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'NotesDatabase',
u'Database server_name!!C:\\Users\\MYNAME\\AppData\\Local\\Lotus\\Notes\\Data\\ma
il202\\myname.nsf has not been opened yet', None, 0, -2147217441), None)
here is my .py file
import win32com.client
import pywintypes
import getpass
def main():
# Get credentials
mailServer = 'server_name'
mailPath = 'C:\Users\MYNAME\AppData\Local\Lotus\Notes\Data\mail202\myname.nsf'
mailPassword = ''
# Connect
notesSession = win32com.client.Dispatch('Lotus.NotesSession')
notesSession.Initialize(mailPassword)
notesDatabase = notesSession.GetDatabase(mailServer, mailPath)
# Get a list of folders
folder = notesDatabase.GetView('$Inbox')
document = folder.GetFirstDocument()
if __name__ == "__main__":
main()
回答1:
Looking at http://www-01.ibm.com/support/docview.wss?uid=swg21308538
The full filepath (e.g. "C:\notes\data\r_apps\haha.nsf") may optionally be used when accessing local databases on a workstation. If you specify a server name, however, or if the code is running on a server, you must use the path relative to the Notes data directory ("r_apps\haha.nsf").
I suggest either (a) not specifying a server or (b) only giving a relative path, ie mailPath = r'mail202\myname.nsf'
.
回答2:
You are using the Notes COM classes. There is a nice shortcut call for opening the current user's mail database. The NotesSession class contains a GetDbDirectory method, which returns a NotesDbDirectory object, and the NotesDbDirectory class contains an OpenMailDatabase method.
I'm not a Python guy so I can't vouch for the exact syntax, but it should be along the lines of this:
notesSession.Initialize(mailPassword)
notesDbDirectory = notesSession.GetDbDirectory('')
notesDatabase = NotesDbDirectory.GetMailDatabase()
Note that the argument to GetDbDirectory can be an empty string or the name of a Domino server. It should not make any difference, as the GetMailDatabase method follows the same procedure as the NotesDatabase.OpenMail method (which is not exposed via the COM interface, therefore it is unavailable to Python). I.e., it looks at the current user's Notes client configuration to locate either a server-based or local replica of the user's mail database.
Also note that if this code is intended to run on one machine but process mail for many users on one Domino server, then you can't use the GetMailDatabase method. In that case, using the relative path as in @Hugh-Bothwell's answer would be correct, although I would strongly recommend adding some defensive programming via a call to notesDatabase.IsOpen() in between the calls to the GetDatabase()
and GetView()
.
来源:https://stackoverflow.com/questions/34659861/python-how-can-i-access-lotus-notes-8-5-inbox-to-read-emails