dongxiaoling1029

导航

[转]邮件

在SMTP服务器,相应帐号已知的情况下,用C#发送电子邮件以有现成的函数,但对于使用Lotus Notes的网络中,如何发邮件,以下是一种方法供参考。

1.保证本地有一个Lotus Notes 的客户端,通过这个客户端,你可以得到三个信息,
     1.服务器的描述, 例如:srvc7/srvc/cag
      2.帐号文件信息 例如:mail102\zhangsan1.nsf
      3.密码 例如:MyPassword
      另外假设你的邮箱是:zhangsan1@mydomain.com
2. 在启动VS2005项目,添加引用,添加COM引用,由于你已经安装了lotus notes客户端,你可以在COM引用中看到Lotus Notes的引用,添加它,你得饮用中会多一个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", zhangsan1@mydomain.com);
                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();
            }

        }

posted on 2011-06-08 20:43  dongxiaoling1029  阅读(316)  评论(0编辑  收藏  举报