Use Python to send and read Lotus Notes Mail __python

Source: Internet
Author: User
use Python to send and read Lotus Notes messages

I original, reprint please indicate the source
blog:why so serious
Github:leoluo22
CSDN: My CSDN 0x00 preface

Company limited internal access to the Internet, only Lotus Notes with the only access to the extranet, so all interactions with the outside world can only be done through Notes mail. In order to remind, the implementation of the automatic mail delivery function. But I also want to send the instructions to the machine via the extranet mail, such as shutdown, clock, reboot and so on. To do this, you can read the contents of the Notes mail, and then google it again and find that most of them are based on VB and. NET, I try to use their code in Python, but there will be a variety of exceptions, so put down. I've been busy for two months and have no energy to pay attention to this. Until last night, due to the company on duty, there is time to study.
Let's talk about the environment:

Os:windows 7 Enterprise 64Bit
Lotus Notes 8.5.3
Python 3.5.3 0x01 Ready

The first thing to understand is two concepts: Mail server database files

These two things are important. The Notes mail database is stored as a. nsf file on the server, so to read the message content, you must need the server name and the. nsf file path. The two can be found by following these steps. Select the file-> preferences-> Online (based on your default)
Click on the Edit Select server option to get your server name
Select mail options to get your file path

Second, notes only provide a COM interface, so you need to build the library first.

From win32com.client import makepy
makepy. Generatefromtypelibspec (' Lotus Domino Objects ')
makepy. Generatefromtypelibspec (' Lotus Notes automation Classes ')
0x02 Read Mail

First, we create a Notesmail object and then initialize the connection in the init method.

Class Notesmail (): "
 send read mail-related actions" "
def __init__ (self, server, file):" "
    Initialize the connection
        @param Server
         server name
        @param file
         data file name
    "" Session
    = Dispatchex (' notes.notessession ')
    server = Session. Getenvironmentstring ("MailServer", True)
    self.db = session. Getdatabase (server, file)
    self.db.OPENMAIL
    self.myviews = []

Self.myviews saves all views under the database.

We can get all the view names:

    def get_views (self): for
    view in self.db.Views:
        if view. Isfolder:
            self.myviews.append (View.name)

The returned contents are as follows:

[Group calendar] ', ' (rule) ', ' (alarms) ′,′ (alarms) ', ' (mapiusecontacts) ', ' (inbox−categorized1) ′,′ (inbox-categorized1) ', ' ( junkmail) ', ' (Trash) ′,′ (oamail) ′,′ (Trash) ', ' (OA Mail) ', ' (Inbox) ', ' Files ', ' SVN ', ' Work ', ' Mine ']

Include your Inbox and the folder you created.

We can define a method to get all the document in a view.

    def make_document_generator (self, view_name):
    self.__get_folder ()
    folder = Self.db.GetView (view_name)
    If not folder:
        raise Exception (' folder {0} is not found. '. Format (view_name)
    document = folder. Getfirstdocument while
    document:
        yield document
        document = folder. GetNextDocument (document)

A pit was trampled here, and at first I wrote

Document = folder. Getfirstdocument ()

Give me an error.

<class ' Win32com.client.CDispatch ' >
traceback (most recent call last):
File "notesmail.py", line, in & lt;module>
Main ()
file "notesmail.py", line, in main
mail.read_mail ()
file "notesmail.py", line Read_mail for document in
    self.make_document_generator (' Mine '):
File "notesmail.py",    Line Make_document_generator
    document = folder. GetNextDocument ()
File "<comobject <unknown>>", Line 2, in GetNextDocument
pywintypes.com_ Error: (-2147352571, ' type does not match. ', None, 0)

Again this kind of mistake, originally rewrote. NET code, this error occurs. The reason for this error may be that there is no getfirstdocument () method. This is not a good day for the Buddha to jump the jump, using the Bing International version to search the Lotus Notes API, the results did not find any useful information. I also search getfirstdocument, a turnaround came, found Lotus's official website, there are a variety of APIs. Here's a recommendation: API

It turns out that you don't need parentheses.

Then write a method to parse the document.

    def extract_documet (self, Document): "" Extract Document "" "Result
    = {}
    result[' subject ' = document. GetItemValue (' Subject ') [0].strip ()
    result[' Date '] = document. GetItemValue (' posteddate ') [0]
    result[' from '] = document. GetItemValue (' from ') [0].strip ()
    result[' to '] = document. GetItemValue (' SendTo ')
    result[' body ' = document. GetItemValue (' body ') [0].strip ()

And,voila.

' Body ': ' tangle \ r \ n Self-iphone ', ' Date ': Pywintypes.datetime (2017, 9, Tzinfo=timezoneinfo (' GMT Standard time '), True), ' Subject ': ',
0x03 Send mail

The operation of sending a message is relatively simple, directly on the code.

    def send_mail (self, receiver, subject, Body=none): "" "
    send a message
        @param receiver: Recipient
        @param Subject: Theme
        @ Param body: Content
    "" "
    doc = Self.db.CREATEDOCUMENT
    doc.sendto = receiver
    Doc. Subject = Subject
    if body:
        Doc. BODY = Body
    doc. SEND (0, receiver)

Space is limited, the complete code is placed in my github.
Welcome to discuss and mention issue.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.