【Lotus Notes】邮件获取

public class LotusManager
    {
        public static int bodyMaxLength, length;
        public static List<Entity.LotusMail> GetEmails(string psw,StringBuilder itemInfo,Action<OnGetEMail> onEMailGet) {
            Domino.NotesSession ns = new Domino.NotesSession();
            if (ns != null) {
                try
                {
                    ns.Initialize(psw);
                }
                catch {
                    throw new Exception("Lotus Notes密码错误!");
                }
                Domino.NotesDocumentCollection all = ns.GetDbDirectory("DominoT/TEST").OpenMailDatabase().AllDocuments;
                Domino.NotesDocument nd = all.GetLastDocument();
                List<Entity.LotusMail> mails = new List<Huawei.Yom.Lotus.Entity.LotusMail>();
                //Entity.LotusMail mail;
                //object[] items;
                //Domino.NotesEmbeddedObject file;
                //int count = 0;
                while (nd != null)
                {
                    #region 废弃代码
                    //mail = new Huawei.Yom.Lotus.Entity.LotusMail();
                    //mail.Subject = ((object[])nd.GetItemValue("Subject"))[0].ToString();
                    //mail.From = ((object[])nd.GetItemValue("From"))[0].ToString();
                    //mail.Body = ((object[])nd.GetItemValue("Body"))[0].ToString();
                    //mail.Date = ((object[])nd.GetItemValue("PostedDate"))[0].ToString();
                    //mail.Size = nd.Size.ToString();
                    //mail.NoteID = nd.NoteID;
                    //if (nd.Items == null)
                    //{
                    //    continue;
                    //}
                    //items = (object[])nd.Items;
                    //if (items == null)
                    //{
                    //    continue;
                    //}
                    //foreach (Domino.NotesItem item in items)
                    //{
                    //    itemInfo.AppendFormat("#{0}#", item.Name);
                    //    file = null;

                    //    if (item.Name == "$FILE")
                    //    {
                    //        try
                    //        {
                                
                    //            file = nd.GetAttachment(((object[])item.Values)[0].ToString()) as Domino.NotesEmbeddedObject;
                    //        }
                    //        catch
                    //        {

                    //        }
                    //        if (file != null)
                    //        {
                    //            if (mail.Attachments == null)
                    //            {
                    //                mail.Attachments = new List<Domino.NotesEmbeddedObject>();
                    //            }
                    //            mail.Attachments.Add(file);

                    //        }
                    //        break;
                    //    }
                    //}
                    //if (count++ >= 10)
                    //{
                    //    break;
                    //}
                    #endregion
                    mails.Add(GetMailFromNotesDocument(nd));
                    if (onEMailGet != null) {
                            onEMailGet(new OnGetEMail()
                            {
                                count = all.Count,
                                currentIndex = mails.Count-1,
                                email = mails[mails.Count - 1]
                            });
                        }
                    nd = all.GetPrevDocument(nd);
                }
                return mails;
            }
            return null;
            //Domino.NotesDocumentClass nddc = new Domino.NotesDocumentClass();
            //return nddc.OpenMailDatabase().AllDocuments.Count;
        }

        internal static Entity.LotusMail GetMailFromNotesDocument(Domino.NotesDocument nd)
        {
            
            Huawei.Yom.Lotus.Entity.LotusMail mail = new Huawei.Yom.Lotus.Entity.LotusMail();
            mail.Subject = ((object[])nd.GetItemValue("Subject"))[0].ToString();
            mail.From = ((object[])nd.GetItemValue("From"))[0].ToString();
            mail.Body = ((object[])nd.GetItemValue("Body"))[0].ToString();
            //mail.Body = ((Domino.NotesItem)(((object[])nd.Items)[13])).Text;
            //mail.Body = ((object[])nd.GetItemValue("wBody"))[0].ToString();
            //mail.Date = ((object[])nd.GetItemValue("PostedDate"))[0].ToString();
            //if (string.IsNullOrEmpty(mail.Date)) {
            //    //$Revisions
            //    mail.Date = ((object[])nd.GetItemValue("$Revisions"))[0].ToString();
            //}
            //mail.Date = nd.Created.ToString();
            mail.Date = nd.LastModified.ToString();
            mail.Size = nd.Size.ToString();
            mail.NoteID = nd.NoteID;
            object[] items = (object[])nd.Items;
            bool isBodyGet=false,isDateGet=false;
            //foreach (Domino.NotesItem item in items)
            //{
            //    if (item.Name!="" && !string.IsNullOrEmpty(item.Text))
            //    {
            //        //mail.Body = item.Text;
            //        //break;
            //    }
            //}
            foreach (Domino.NotesItem item in items)
            {
                if (item.Name == "Body") {
                    mail.Body = item.Text;
                    //break;
                    isBodyGet=true;
                }
                if (item.Name == "DeliveredDate")
                {
                    mail.Date = item.Text;
                    isDateGet = true;
                }
                if (isBodyGet && isDateGet) {
                    break;
                }
            }
            //if (mail.Body.IndexOf("注意安全!!!目前广州、深圳常见的街头骗术!!") != -1)
            //{
            //    mail.Body = ((Domino.NotesItem)(((object[])nd.Items)[13])).Text;
            //foreach (Domino.NotesItem item in items)
            //{
            //    if (item.Name == "Body" && !string.IsNullOrEmpty(item.Text))
            //    {
            //        //mail.Body = item.Text;
            //        //break;
            //    }
            //}
            //}
            int bl = mail.Body.Length;

            if (bl > length)
            {
                int page = Convert.ToInt32(Math.Ceiling(bl * 1.0 / length));
                bodyMaxLength = Math.Max(page, bodyMaxLength);
                mail.WBody = new string[page];
                for (int i = 0; i < page; i++)
                {
                    mail.WBody[i] = mail.Body.Substring(i * length, bl - i * length >= length ? length : bl - i * length);
                }
            }
            else {
                mail.WBody = new[] { mail.Body };
            }
            return mail;
        }

        //public static int ByteLength(string str)
        //{
        //    byte[] bytestr = System.Text.Encoding.Unicode.GetBytes(str);
        //    int j = 0;
        //    for (int i = 0; i < bytestr.GetLength(0); i++)
        //    {
        //        if (i % 2 == 0)
        //        { j++; }
        //        else
        //        {
        //            if (bytestr[i] > 0)
        //            { j++; }
        //        }
        //    }
        //    return j;
        //}
    }
public class LotusMail
    {
        public string NoteID
        {
            get;
            set;
        }
        public string Subject
        {
            get;
            set;
        }
        public string From
        {
            get;
            set;
        }
        public string Date
        {
            get;
            set;
        }
        public string Time
        {
            get;
            set;
        }
        public string Size
        {
            get;
            set;
        }
        public string Body
        {
            get;
            set;
        }
        public List<Domino.NotesEmbeddedObject> Attachments
        {
            get;
            set;
        }
        public string Other
        {
            get;
            set;
        }
        public string[] WBody
        {
            get;
            set;
       

 

上面是即时文件获取

下面是NSF文件里邮件获取

public class OnGetEMail {
        public int count;
        public int currentIndex;
        public Entity.LotusMail email;
    }
    public class NsfDB
    {
        static Domino.NotesSession ns;
        static Domino.NotesDatabase ndb;
        public static void CreatSession(string psw) {
            if (ns == null) {
                ns = new Domino.NotesSession();
                try
                {
                    ns.Initialize(psw);
                }
                catch
                {
                    throw new Exception("Lotus Notes密码错误!");
                }
            }
        }
        public static void Dispose(){
            if (ns != null)
            {
                ns = null;
            }
            if (ndb != null)
            {
                ndb = null;
            }
        }
        public static List<Entity.LotusMail> GetEmailData(string filename, string psw, Action<OnGetEMail> onEMailGet)
        {
            CreatSession(psw);
            if (ns != null)
            {
                ndb = ns.GetDatabase(null, filename, false);
                Domino.NotesDocumentCollection all =ndb.AllDocuments;
                ndb = null;
                Domino.NotesDocument nd = all.GetLastDocument();
                List<Entity.LotusMail> mails = new List<Huawei.Yom.Lotus.Entity.LotusMail>();
                while (nd != null)
                {
                    try
                    {
                        mails.Add(LotusManager.GetMailFromNotesDocument(nd));
                        if (onEMailGet != null) {
                            onEMailGet(new OnGetEMail()
                            {
                                count = all.Count,
                                currentIndex = mails.Count-1,
                                email = mails[mails.Count - 1]
                            });
                        }
                    }
                    catch {
                    }
                    nd = all.GetPrevDocument(nd);
                }
                return mails;
           
            }
            return null;
            
        }
        public static object GetEmailDataTest(string filename,string psw)
        {
            Domino.NotesSession ns = new Domino.NotesSession();
            if (ns != null)
            {
                ns.Initialize(psw);
                NotesDatabase nd = ns.GetDatabase(null, filename, false);
                return nd.AllDocuments.Count;

            }
            return null;

        }
    }

 此例子需要引用两个COM组件:

  1. Lotus Domino Objects
  2. Lotus Notes Automation Classes

 

posted @ 2013-07-18 08:55  哈哈好玩  阅读(1153)  评论(0编辑  收藏  举报