[Domino]Java访问Domino邮件代码片断[1]

 

[Domino]Java访问Domino邮件代码片断

编写者

日期

关键词

郑昀@ultrapower

2005-6-20

Java Domino

 

得到用户收件箱中的邮件

三个知识点:

1:如果是打开mailfile数据库后直接

Database dbMail = sNotes.getDatabase(sNotes.getServerName(),

                      mailfile, false);

DocumentCollection dc = dbMail.getAllDocuments();

Document doc = dc.getFirstDocument();

这样打开的是用户邮箱数据库中全部邮件。

如果是选择了$Inbox视图,才会只浏览收件箱中的所有邮件,如下面的代码所示。

2:程序模拟用户身份访问邮箱,其中的所有邮件都会是新邮件,除非自己做标记。

3:关于邮件的日期,最好“delivereddate”和“posteddate”两个字段都取出来,这样保险。

代码片段:

Database dbMail = sNotes.getDatabase(sNotes.getServerName(),

                      mailfile, false);

if(dbMail == null)

    {

              System.out.println("cannot open database " + mailfile);

    }

    else

    {

           // 如果按照下面的dc.getFirstDocument,可能连垃圾箱中的未读邮件都取出来了

           View viewInbox = dbMail.getView("($Inbox)");

           // 所以我们得到Inbox视图,这样只读取收件箱中的邮件

           Document doc = viewInbox.getFirstDocument();

          

    // 此处的新邮件数量,并不是用户自身Notes看到的新邮件数量,因为身份不同;

    // 当我们的程序第一次访问用户邮箱,所有的邮件将都是新邮件

//Document doc = dc.getFirstDocument();

 

while (doc != null)

{

           /** 添加新邮件项的节点 */

           Element mailElement = mailsElement.addElement("mail");

         

           // 发件人

           Element fromE = mailElement.addElement("from");

           fromE.addCDATA(doc.getItemValueString("Principal"));

           // 收件日期

           Element dateE = mailElement.addElement("date");

           Item deliveredDate = doc.getFirstItem( "delivereddate" ) ;

           Item     postedDate    = doc.getFirstItem( "posteddate" ) ;

           String   timestamp     = new String( "" ) ;

           if ( deliveredDate != null )

               timestamp = deliveredDate.getText() ;

            else if ( postedDate != null )

               timestamp = postedDate.getText() ;

           // 发送日期和接收日期,我们都要进行读取,发送日期是优先的

           dateE.addCDATA(timestamp);

           // 主题

           Element subjectE = mailElement.addElement("subject");

           subjectE.addCDATA(doc.getItemValueString("Subject"));

           // NoteID

           Element NoteIDE = mailElement.addElement("NoteID");

           NoteIDE.setText(doc.getNoteID());

           // UniversalID

           Element UniversalIDE = mailElement.addElement("UniversalID");

           UniversalIDE.setText(doc.getUniversalID());

          

           String strBody = "";

           if(doc.getItemValueString("Body") != null)

               {

                  strBody = doc.getItemValueString("Body");

                  if(strBody.length() > 12)

                 {

                         strBody = strBody.substring(12) + "...";

                 }

               }

               else

               {

                      strBody = "没有正文";

               }

}

取到邮件的附件集合

 

代码片段:

try

                          {

                                 // grab the Body field of the current document,

                                    // and check each attachment

                                 RichTextItem bodyWithAttachs =

                                        (RichTextItem)doc.getFirstItem("Body");

                                 System.out.println("-= begin getFirstItem =-");

                                 // 查看附件

                                 if(bodyWithAttachs != null)

                                 {                                 

                                        System.out.println("-= get RichTextItem =-");

                                        

                                        Vector vAttachs = bodyWithAttachs.getEmbeddedObjects();

                                        System.out.println("-= get Vector =-");

                                        Enumeration eAttachs = vAttachs.elements();

                                        System.out.println("-= get Enumeration =-");

                                       

                                        // 读取共有多少个附件

                                        Element attachmentsE = mailElement.addElement("attachments");

                                        Element countE = attachmentsE.addElement("count");

                                        countE.setText(String.valueOf(vAttachs.size()));

 

                                        while (eAttachs.hasMoreElements())

                                        {

                                               EmbeddedObject eo = (EmbeddedObject)eAttachs.nextElement();

                                               if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT)

                                               {

                                                // 添加附件名称的节点

                                                   Element attachE = attachmentsE.addElement("attachment");

                                                   attachE.setText(eo.getSource());

                                               }

                                               eo.recycle();

                                        }

                                 }

                             }

                          catch(Exception e)

                               {

                                      if(e instanceof lotus.domino.NotesException)

                                      {

                                             System.out.println(

                                                           "访问邮件的附件时发生错误,错误代码为"

                                                     +((lotus.domino.NotesException)e).id);

                                      }

                                   e.printStackTrace();

                                   System.out.println(e.getLocalizedMessage());

                               }

 

 

编写者

日期

关键词

郑昀@ultrapower

2005-6-20

Java Domino

 

你可能感兴趣的:(java)