搜索 | 会员  
使用c#和LotusDominoObjectsCom访问IBMLotusNotes,发
来源:    作者:网友  日期:2011/8/10  类别:编程语言  主题:.NET  编辑:dezai
1.本机安装Lotus Notes客户端。 2.注册组件:regsvr32 quot;C:\Program
 


1.本机安装Lotus Notes客户端。

2.注册组件:regsvr32 "C:\Program Files\lotus\notes\nlsxbe.dll"。

3.打开vs2010,新建ASP.NET Web应用程序,取名“NotesMail”。

4.添加引用,选 “com” 找到“Lotus Domino Objects”,点 “确定”。

5.新建文件夹 “EmailTemplate”,新建 HTML页,取名 “DefaultTemplate.htm”,在<body></body>里写html代码。例如:

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <title></title>
   </head>
   <body>
   Dear {0},
   您好!<br/>
   <p>{1}</p>
   请点击连接:<a href="http://{2}/default.aspx">報修單查詢</a><br />
   <p>此為系統郵件,請勿回覆!</p>
   </body>
   </html>
 
 
6.新建一个类 “EmailTemplateGenerate.cs”用于读取文件

   using System;
   using System.Web;
   using System.IO;
   
   namespace NotesMail
   {
       public class EmailTempGenerate
       {
           public string GetTemplate(string path)
           {
               path = HttpContext.Current.Server.MapPath(path);
               using (StreamReader sr = new StreamReader(path))
               {
                   try
                   {
                       string result = sr.ReadToEnd();
                       return result;
                   }
                   catch (Exception err)
                   {
                       throw new Exception(err.Message);
                   }
               }
           }
       }
   }
 
 
7.新建一个类 “Common”,再建一个静态方法 “AppPath” 用于读取IP/主机名+应用程序根路径

  using System.Web;
  
  namespace NotesMail
  {
      public class Common
      {
          public static string AppPath
          {
              get
              {
                  return HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
              }
          }
      }
  }
 
8.为测试,在 Default.aspx上放置一个 Button,起名为 “btn_Send”,双击添加click事件

9.打开 “Default.aspx.cs” ,编写代码,实现发送HTML邮件

步骤1:添加引用 using Domino;

步骤2:定义

   NotesSession ns;
   NotesDatabase db;
   NotesDocument doc;
  
步骤3:写“btn_Send_Click”

       protected void btn_Send_Click(object sender, EventArgs e)
       {
           try
           {
               ns = new NotesSession();
               if (ns != null)
               {
                   //您本机notes的密码
                   ns.Initialize("password");
                   //初始化NotesDatabase
                   db = ns.GetDatabase("Domino服务器名称", "names.nsf", false);
                   doc = db.CreateDocument();
                   doc.ReplaceItemValue("Form", "Memo");
                   doc.ReplaceItemValue("SendTo", "Notes用户名");                  
                   doc.ReplaceItemValue("Subject", "通知邮件");
                   //html内容的邮件
                   ns.ConvertMime = false;                   
                   EmailTempGenerate etg = new EmailTempGenerate();
                   string temp = etg.GetTemplate(ResolveUrl("~/EmailTemplate/DefaultTemplate.htm"));
                   string htmlbody = string.Format(temp, "尊敬的用户", "您的物品维修完成", Common.AppPath);
                   NotesStream stream = ns.CreateStream();
                   stream.WriteText(htmlbody, EOL_TYPE.EOL_ANY);               
                   NotesMIMEEntity mimebody = doc.CreateMIMEEntity("Body");  
                   NotesMIMEEntity mimehtml = mimebody.CreateChildEntity(null);                  
                   mimehtml.SetContentFromText(stream, "text/html; charset=UTF-8", MIME_ENCODING.ENC_NONE);
                   stream.Truncate();
                   stream.Close();
                   doc.CloseMIMEEntities(true, "Body");
                   ns.ConvertMime = true;
                   doc.ComputeWithForm(false, false);                
                   doc.Send(false);
                   ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('Message Send');", true);
               }
           }
           catch (Exception ex)
           {
               string msg = "alert('" + ex.Message + "');";
               ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", msg, true);
           }
           finally
           {
               ns = null;
               db = null;
               doc = null;
           }
       }
如果想获取notes用户的信息,如用户名,internet地址等,方便起见,页面上添加一个listbox用于显示,名称为: “lbx_to” 方法如下:

 NotesSession ns;
 NotesDatabase db;
 NotesDocument doc;
 NotesView vw;
 
 protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
             {
                 init();               
             }
         }
 void init()
         {
             try
             {
                 ns = new NotesSession();
                 ns.Initialize("eola");
                 db = ns.GetDatabase("Domino服务器名称", "names.nsf", false);
                 if (db != null)
                 {
                     vw = db.GetView("People");
                     doc = vw.GetFirstDocument();
                     while (doc != null)
                     {
                         var firstname = doc.GetFirstItem("FirstName");
                         var lastname = doc.GetFirstItem("LastName");
                         var InternetAddress = doc.GetFirstItem("InternetAddress");
                         if (firstname != null && lastname != null && InternetAddress != null)
                         {
                             string temp = InternetAddress.Values[0] as string;
                             if (!string.IsNullOrWhiteSpace(temp))
                             {
                                 lbx_to.Items.Add("用户名:"+lastname.Text + "        邮件地址:" + temp.Trim());
                             }
                         }
                         doc = vw.GetNextDocument(doc);
                     }
                 }
             }
             catch (Exception ex)
             {
                 return;
             }
             finally
             {
                 ns = null;
                 doc = null;
                 db = null;
             }
 
         }
 

德仔网尊重行业规范,每篇文章都注明有明确的作者和来源;德仔网的原创文章,请转载时务必注明文章作者和来源:德仔网;
头条那些事
大家在关注
广告那些事
我们的推荐
也许感兴趣的
干货