小木可 2010-06-17
今天在公司整理Domino代码时,顺手写了几个测试代码,晚上回顾时,发现基本涵盖了邮件操作的相关方面,记录之,供以后参考。
依赖JAR包:NCSO.jar
依赖条件:Domino邮件服务器开通DIIOP端口
代码很简单,直接复制,有不明白的查询相关API即可。
@Test public void testReceiveMail(){ String dominoServer = "192.168.0.252"; String username = "cyy"; Session session = null; Database database = null; View view = null; try { session = NotesFactory.createSession(dominoServer, NotesFactory.createORB(), username, "1111"); System.out.println("connect to mail server ok" ); database = session.getDatabase(session.getServerName(), "mail//" + username + ".nsf", false); view = database.getView("($Inbox)"); //获得第一封邮件的unid Document doc = view.getFirstDocument(); String unid = doc.getUniversalID(); //通过具体的unid,获得对应的邮件信息 doc = database.getDocumentByUNID(unid); System.out.println(doc.generateXML()); System.out.println("mail from:" + doc.getItemValueString("Principal")); System.out.println("mail content:" + doc.getItemValueString("Body")); System.out.println("mail subject:" + doc.getItemValueString("Subject")); //处理附件 saveAttachment(doc); } catch (Exception ex) { System.out.println("connect to mail server fail" ); }finally{ if(view!=null){ try { view.recycle(); } catch (NotesException e) { e.printStackTrace(); } } if(database!=null){ try { database.recycle(); } catch (NotesException e) { e.printStackTrace(); } } if(session!=null){ try { session.recycle(); } catch (NotesException e) { e.printStackTrace(); } } } }
接下来是保存邮件附件到本地的代码:
public void saveAttachment(Document doc) throws NotesException, FileNotFoundException{ RichTextItem bodyWithAttachs = (RichTextItem) doc.getFirstItem("Body"); if (bodyWithAttachs != null) { Vector vAttachs = bodyWithAttachs.getEmbeddedObjects(); Enumeration eAttachs = vAttachs.elements(); while (eAttachs.hasMoreElements()) { EmbeddedObject eo = (EmbeddedObject) eAttachs.nextElement(); if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT) { String storePath = "D:/"; if (!StringUtils.isEmpty(eo.getName())) { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(storePath + eo.getName(), false)); BufferedInputStream bufferedinputstream = new BufferedInputStream(eo.getInputStream()); int n; try { while ((n = bufferedinputstream.read()) != -1) { out.write(n); } out.flush(); out.close(); bufferedinputstream.close(); } catch (IOException ex1) { } } } } } }
发送邮件我们分为两类,第一个是不带附件的简单实现,第二个是带多个附件的实现。
@Test public void testSendMail(){ String dominoServer = "192.168.0.2"; String username = "cxx"; Session session = null; Database database = null; try { //获取session session = NotesFactory.createSession(dominoServer, NotesFactory.createORB(), username, "1111"); System.out.println("connect to mail server ok" ); //得到数据库 database = session.getDatabase(session.getServerName(), "mail//" + username + ".nsf", false); Document doc = database.createDocument(); //设置发件人 doc.appendItemValue("Principal", "[email protected]"); //设置主题 doc.appendItemValue("Subject", "我是主题"); //设置收件人 Vector<String> toVect = new Vector<String>(); toVect.add("[email protected]"); doc.send(toVect); } catch (Exception ex) { System.out.println("connect to mail server fail" ); }finally{ if(database!=null){ try { database.recycle(); } catch (NotesException e) { e.printStackTrace(); } } if(session!=null){ try { session.recycle(); } catch (NotesException e) { e.printStackTrace(); } } } }
@Test public void testSendMailWithAttachment(){ String dominoServer = "192.168.0.2"; String username = "cxx"; Session session = null; Database database = null; try { //获取session session = NotesFactory.createSession(dominoServer, NotesFactory.createORB(), username, "1111"); System.out.println("connect to mail server ok" ); //得到数据库 database = session.getDatabase(session.getServerName(), "mail//" + username + ".nsf", false); Document doc = database.createDocument(); //设置发件人 doc.appendItemValue("Principal", "[email protected]"); //设置主题 doc.appendItemValue("Subject", "我是主题"); //设置收件人 Vector<String> toVect = new Vector<String>(); toVect.add("[email protected]"); doc.appendItemValue("Form", "Memo"); RichTextItem rti = doc.createRichTextItem("Body"); rti.addNewLine(2); rti.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null, "D:/test.png", "D:/test.png"); rti.addNewLine(2); rti.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null, "D:/test.txt", "D:/test.txt"); //开始发送 doc.send(true,toVect); } catch (Exception ex) { System.out.println("connect to mail server fail" ); }finally{ if(database!=null){ try { database.recycle(); } catch (NotesException e) { e.printStackTrace(); } } if(session!=null){ try { session.recycle(); } catch (NotesException e) { e.printStackTrace(); } } } }
至于复杂的操作,基本都可以从如上代码中类推出来,或者百度、google一下。