分享

C#简单操作Lotus Notes邮件

 king9413 2012-04-27
前段时间简单的研究了一下.NET操作Lotus Notes邮件的实现,具体的操作包括邮件的读取和发送,而且都要包含附件,其间参考了《在 Microsoft .NET 应用程序中使用 IBM Lotus Domino》一文,现在把成果和大家分享一下。
本文将分为获取用户列表、发送邮件、收取邮件三个部分,并会在文末提供范例程序(Visual Studio 2008)的下载。

引用

如果想使用.NET操作Lotus,我们可以使用 Lotus Domino Objects 通过 COM 来访问 Domino 对象,在 Domino 服务器或者任何一个 Notes 客户机(IBM Lotus Domino Designer、Lotus Domino Administrator 或者 Lotus Notes)的安装中都包括 Lotus Domino Objects。如果你的开发环境复合上述的要求,就可以添加一个COM引用:Lotus Domino Objects,然后:
using Domino;
如果找不到这个COM组件,可以先注册以下组件,然后就可以找到了:

regsvr32 "C:\Program Files\lotus\notes\nlsxbe.dll"


获取Notes邮箱用户列表

本段代码将遍历用户视图(People View)中的所有用户,将其全名添加到ComboBox控件中。新建两个全局变量(全局的目的是供本例中的其它方法使用)。
NotesSession ns;
NotesDatabase ndb;

发送邮件

本段代码降为从ComboBox中选中的用户发送一封邮件,我们可以输入邮件的标题和内容,并可以添加附件。
try
{
    
if(ns!=null)
    {
        NotesDocument doc 
= ndb.CreateDocument();

        doc.ReplaceItemValue(
"Form""Memo");

        
//收件人信息
        doc.ReplaceItemValue("SendTo", cb_People.Text);

        
//邮件主题
        doc.ReplaceItemValue("Subject", tb_Subject.Text);

        
//邮件正文
        NotesRichTextItem rt = doc.CreateRichTextItem("Body");
        rt.AppendText(tb_Body.Text);

        
//附件
        if (!string.IsNullOrEmpty(tb_Attachment.Text))
        {
            NotesRichTextItem attachment 
= doc.CreateRichTextItem("attachment");
            attachment.EmbedObject(EMBED_TYPE.EMBED_ATTACHMENT, 
"", tb_Attachment.Text, "attachment");
        }

        
//发送邮件
        object obj = doc.GetItemValue("SendTo"); 
        doc.Send(
false,ref obj);

        doc 
= null;
        MessageBox.Show(
"Successfully!");
    }

}
catch(Exception ex)
{
    MessageBox.Show(
"Error:" + ex.Message);
}

如果想要添加多个收件人,可以将代码:
doc.ReplaceItemValue("SendTo", cb_People.Text); 
更改为代码:
string[] receivers = { cb_People.SelectedItem.ToString(), "windie chai/TEST" };
doc.ReplaceItemValue(
"SendTo", receivers);
如果想要添加多个附件,可以继续调用这个对象的EmbedObject方法:
attachment.EmbedObject(EMBED_TYPE.EMBED_ATTACHMENT, """FilePath""attachment"); 

获取收件箱内全部邮件

为了存储邮件内容,我编写了一个简单的类:
简单的邮件类
接着添加一个全局变量来存储邮件集合:
public List<Mail> mails = new List<Mail>();
然后将所有邮件的信息添加的mails集合中,并将它们的标题添加到一个ListBox中。
//清空邮件列表
lb_Mail.Items.Clear();
mails.Clear();

//获取邮箱视图
NotesDbDirectory dir = ns.GetDbDirectory("DominoT/TEST");
NotesDatabase maildb 
= dir.OpenMailDatabase();
NotesView nv 
= maildb.GetView("$inbox");

//遍历所有邮件
NotesDocument doc = nv.GetFirstDocument();
while (doc != null)
{
    Mail mail 
= new Mail();
    mail.Subject 
= ((object[])doc.GetItemValue("Subject"))[0].ToString();
    mail.From 
= ((object[])doc.GetItemValue("From"))[0].ToString();
    mail.Body 
= ((object[])doc.GetItemValue("Body"))[0].ToString();
    mail.Time 
= ((object[])doc.GetItemValue("PostedDate"))[0].ToString();
    
object[] items = (object[])doc.Items;
    
foreach (NotesItem item in items)
    {
        
if (item.Name == "$FILE")
        {
            
string fileName = ((object[])item.Values)[0].ToString();
            NotesEmbeddedObject file 
= (NotesEmbeddedObject)doc.GetAttachment(fileName);
            
if (file != null)
                mail.Files.Add(file);
        }
    }

    mails.Add(mail);
    lb_Mail.Items.Add(mail.Subject);

    
//查找下一封邮件
    doc = nv.GetNextDocument(doc);
}

显示邮件内容并打开附件

由于前面的代码中我已经把邮件信息添加到自己定义的邮件集合中了,所以下面的操作就不需要和Domino服务器交互了。
本段代码实现了在ListBox中点击邮件标题后在一个TextBox中显示邮件内容(包括标题,时间,正文和附件文件名)。 
Mail m = mails[lb_Mail.SelectedIndex];
StringBuilder sbMail 
= new StringBuilder();
sbMail.AppendLine(m.Subject);
sbMail.AppendLine(
"----------");
sbMail.AppendLine(m.Time);
sbMail.AppendLine(
"----------");
sbMail.AppendLine(m.Body);
sbMail.AppendLine(
"----------");
sbMail.AppendLine(
"Attachments:");
foreach (NotesEmbeddedObject file in m.Files)
{
    sbMail.AppendLine(file.Name);
}
tb_Mail.Text 
= sbMail.ToString();

//根据附件数量决定打开附件按钮是否可用
if(m.Files.Count>0)
{
    btn_OpenAttachment.Enabled 
= true;
}
else
    btn_OpenAttachment.Enabled 
= false;
本段代码实现了当点击“打开附件”按钮后从内从中释放附件文件到硬盘并执行它。
Mail m = mails[lb_Mail.SelectedIndex];

//获取第一个附件
NotesEmbeddedObject file = m.Files[0];

//组合一个临时路径
string filename = Path.Combine(Application.StartupPath,file.Name);

//将附件释放到临时路径
file.ExtractFile(filename);

//执行附件
System.Diagnostics.Process.Start(filename);
主要的代码就这么多了,第一次使用IBM的软件,感觉……很不顺手,连对象模型都相当别扭,嗯,废话不说了 ,如果上面的代码有何不恰当的地方还请各位朋友多多指教。
点击这里下载本文的范例程序(Visual Studio 2008),范例程序的UI如下:

点击“Sender”之后回到Notes中,刷新发现邮件已经收到,包含附件,并且可以正常打开:
原文发布于coding.,欢迎访问、订阅并和我交流。
 

進入 " Visual Studio 2005 命令提示"

C:\Lotus\Notes>sn -k LotusNotes.snk

C:\Lotus\Notes>sn -p LotusNotes.snk LotusNotesPublic.snk

C:\Lotus\Notes>sn -t LotusNotesPublic.snk

C:\Lotus\Notes>tlbimp domobj.tlb /keyfile:Lotusnotes.snk

Microsoft (R) .NET Framework Type Library to Assembly Converter 2.0.50727.42

Copyright (C) Microsoft Corporation. All rights reserved.

   

Type library imported to Domino.dll

   

再把Domino.dll 拖到Windows\assembly (GAC) 目錄下.實現部署.

   

   

   

網站中要使用Notes 必須在Server 上進行以下權限設置:

  1. Lotus 目錄進行權限設定:

    Domain Users Users 加上寫的權限.

       

       

   

Tlbimp 命令

C:\Lotus>tlbimp

Microsoft (R) .NET Framework Type Library to Assembly Converter 2.0.50727.42

Copyright (C) Microsoft Corporation. All rights reserved.

   

Syntax: TlbImp TypeLibName [Options]

Options:

/out:FileName File name of assembly to be produced

/namespace:Namespace Namespace of the assembly to be produced

/asmversion:Version Version number of the assembly to be produced

/reference:FileName File name of assembly to use to resolve references

/tlbreference:FileName File name of typelib to use to resolve references

/publickey:FileName File containing strong name public key

/keyfile:FileName File containing strong name key pair

/keycontainer:FileName Key container holding strong name key pair

/delaysign Force strong name delay signing

/unsafe Produce interfaces without runtime security checks

/noclassmembers Prevents TlbImp from adding members to classes

/nologo Prevents TlbImp from displaying logo

/silent Suppresses all output except for errors

/verbose Displays extra information

/primary Produce a primary interop assembly

/sysarray Import SAFEARRAY as System.Array

/machine:MachineType Create an assembly for the specified machine type

/transform:TransformName Perform the specified transformation

/strictref Only use assemblies specified using /reference and

registered PIAs

/strictref:nopia Only use assemblies specified using /reference and

ignore PIAs

/? or /help Display this usage message

   

The assembly version must be specified as: Major.Minor.Build.Revision.

   

Multiple reference assemblies can be specified by using the /reference option

multiple times.

   

Supported machine types:

X86

X64

Itanium

Agnostic

   

Supported transforms:

SerializableValueClasses Mark all value classes as serializable

DispRet Apply the [out, retval] parameter transformation

to methods of disp only interfaces

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多