Notes程式研究

在SMTP伺服器,相應帳號已知的情況下,用C#發送電子郵件以有現成的函數,但對於使用Lotus Notes的網路中,如何發郵件,以下是一種方法供參考

1.保證本地有一個Lotus Notes 的用戶端,通過這個用戶端,你可以得到三個資訊,
       1.伺服器的描述, 例如:srvc7/srvc/cag
        2.帳號檔資訊 例如:mail102\zhangsan1.nsf
        3.密碼 例如:MyPassword

<add key=”NotesDBServerStr” value=”CHNYT03/FOXCONN”/>

<add key=”NotesDBFilePathStr” value=”mail01\ytitsdt.nsf”/>

<add key=”NotesPassword” value=”84403736″/>

2. 在啟動VS2005專案,添加引用,添加COM引用,由於你已經安裝了lotus notes用戶端,你可以在COM引用中看到Lotus Notes的引用(Lotus Domino Objects),添加它,你得飲用中會多一個Domino的dll,
3.在你的項目的命名空間中,加上
using Domino;

4.以下是發送郵件的函數
        public Boolean SendNotesMail(string ToMail, string Subject, string Body)
        {
            Domino.NotesSession oNotesSession = null; ;
            Domino.NotesDatabase oNotesDatabase = null;
            Domino.NotesDocument oNotesDocument = null;
            object oItemValue = null;
            //String sUserName;                         
            String sPassword = “”;           //Password used by COM to pass to the Notes client login.
            String sServerName = “”;
            String sMailFile = “”;

            try
            {
                oNotesSession = new Domino.NotesSession(); //create a notes session object
                //sPassword = MsgData.Password;
                sPassword = “MyPassword”;
                oNotesSession.Initialize(sPassword); //Initialise session by passing a password. Lotus Notes will not load.

                //sUserName = oNotesSession.UserName;
                //Console.WriteLine(sUserName);

                //Create a database handle to the database you wish to send the mail message from.
                //sServerName = MsgData.ServerName;
                sServerName = @”srvc7/srvc/cag”;
                //sMailFile = MsgData.MailFile;
                sMailFile = @”mail102\zhangsan1.nsf”;
                oNotesDatabase = oNotesSession.GetDatabase(sServerName, sMailFile, false);

                //If the database is not already open then open it.
                if (!oNotesDatabase.IsOpen)
                {
                    oNotesDatabase.Open();
                }
                //Create an in memory document in the server database
                oNotesDocument = oNotesDatabase.CreateDocument();
                //Assign Field Values
                oNotesDocument.ReplaceItemValue(“Form”, “Memo”);
                //oNotesDocument.ReplaceItemValue(“From”, MsgData.FromMail);
                oNotesDocument.ReplaceItemValue(“From”, [email protected]);
                oNotesDocument.ReplaceItemValue(“SendTo”, ToMail);
                oNotesDocument.ReplaceItemValue(“Subject”, Subject);
                NotesRichTextItem rt = oNotesDocument.CreateRichTextItem(“Body”);
                rt.AppendText(Body);
                //oNotesDocument.ReplaceItemValue(“Body”, MsgData.Body);
                oNotesDocument.SaveMessageOnSend = true; //Ensure memo shows in sent folder
                oNotesDocument.ReplaceItemValue(“postDate”, DateTime.Now.ToShortDateString());

                //Send requires an object for the recipients, so I give the send method the SendTo field as an object.

                oItemValue = oNotesDocument.GetItemValue(“SendTo”);
                //Send the email
                oNotesDocument.Send(false, ref oItemValue);
                return true;

            }
            catch (Exception error)
            {
                Console.WriteLine(“{0} My Exception caught.”, error);
                return false;
            }
            finally
            {
                //if (oNotesSession != null) { Marshal.ReleaseComObject(oNotesSession); }
                //if (oNotesDatabase != null) { Marshal.ReleaseComObject(oNotesDatabase); }
                //if (oNotesDocument != null) { Marshal.ReleaseComObject(oNotesDocument); }
                //if (oItemValue != null) { Marshal.ReleaseComObject(oItemValue); }

                oNotesSession = null;
                oNotesDatabase = null;
                oNotesDocument = null;
                oItemValue = null;
                GC.Collect();
            }

        }

你可能感兴趣的:(Lotus)