java收发邮件--Domino服务器

依赖JAR包:NCSO.jar

依赖条件:Domino邮件服务器开通DIIOP端口

收取邮件

  1. public void testReceiveMail(){  
  2.     String dominoServer = "192.168.0.252";  
  3.     String username = "cyy";  
  4.     Session session = null;  
  5.     Database database = null;  
  6.     View view = null;  
  7.     try {  
  8.         session = NotesFactory.createSession(dominoServer, NotesFactory.createORB(), username, "1111");  
  9.         System.out.println("connect to mail server ok" );  
  10.         database = session.getDatabase(session.getServerName(), "mail//" + username + ".nsf"false);  
  11.         view = database.getView("($Inbox)");  
  12.         //获得第一封邮件的unid  
  13.         Document doc = view.getFirstDocument();  
  14.         String unid = doc.getUniversalID();  
  15.         //通过具体的unid,获得对应的邮件信息  
  16.         doc = database.getDocumentByUNID(unid);  
  17.         System.out.println(doc.generateXML());  
  18.           
  19.         System.out.println("mail from:" + doc.getItemValueString("Principal"));  
  20.         System.out.println("mail content:" + doc.getItemValueString("Body"));  
  21.         System.out.println("mail subject:" + doc.getItemValueString("Subject"));  
  22.         //处理附件  
  23.         saveAttachment(doc);  
  24.     } catch (Exception ex) {  
  25.         System.out.println("connect to mail server fail" );  
  26.     }finally{  
  27.         if(view!=null){  
  28.             try {  
  29.                 view.recycle();  
  30.             } catch (NotesException e) {  
  31.                 e.printStackTrace();  
  32.             }  
  33.         }  
  34.         if(database!=null){  
  35.             try {  
  36.                 database.recycle();  
  37.             } catch (NotesException e) {  
  38.                 e.printStackTrace();  
  39.             }  
  40.         }  
  41.         if(session!=null){  
  42.             try {  
  43.                 session.recycle();  
  44.             } catch (NotesException e) {  
  45.                 e.printStackTrace();  
  46.             }  
  47.         }  
  48.     }  

保存邮件附件到本地:

  1. public void saveAttachment(Document doc) throws NotesException, FileNotFoundException{  
  2.     RichTextItem bodyWithAttachs = (RichTextItem) doc.getFirstItem("Body");  
  3.     if (bodyWithAttachs != null) {  
  4.         Vector vAttachs = bodyWithAttachs.getEmbeddedObjects();  
  5.         Enumeration eAttachs = vAttachs.elements();  
  6.         while (eAttachs.hasMoreElements()) {  
  7.             EmbeddedObject eo = (EmbeddedObject) eAttachs.nextElement();  
  8.             if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT) {  
  9.                 String storePath = "D:/";  
  10.                 if (!StringUtils.isEmpty(eo.getName())) {  
  11.                     BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(storePath + eo.getName(), false));  
  12.                     BufferedInputStream bufferedinputstream = new BufferedInputStream(eo.getInputStream());  
  13.                     int n;  
  14.                     try {  
  15.                         while ((n = bufferedinputstream.read()) != -1) {  
  16.                             out.write(n);  
  17.                         }  
  18.                         out.flush();  
  19.                         out.close();  
  20.                         bufferedinputstream.close();  
  21.                     } catch (IOException ex1) {  
  22.                     }  
  23.                       
  24.                 }  
  25.             }  
  26.         }  
  27.     }  

不带附件发送邮件

  1. public void testSendMail(){  
  2.     String dominoServer = "192.168.0.2";  
  3.     String username = "cxx";  
  4.     Session session = null;  
  5.     Database database = null;  
  6.     try {  
  7.         //获取session  
  8.         session = NotesFactory.createSession(dominoServer, NotesFactory.createORB(), username, "1111");  
  9.         System.out.println("connect to mail server ok" );  
  10.         //得到数据库  
  11.         database = session.getDatabase(session.getServerName(), "mail//" + username + ".nsf"false);  
  12.         Document doc = database.createDocument();  
  13.         //设置发件人  
  14.         doc.appendItemValue("Principal""cxx@testdomain.com");  
  15.         //设置主题  
  16.         doc.appendItemValue("Subject""我是主题");  
  17.         //设置收件人  
  18.         Vector toVect = new Vector();  
  19.         toVect.add("ggg@testdomain.com");  
  20.         doc.send(toVect);  
  21.     } catch (Exception ex) {  
  22.         System.out.println("connect to mail server fail" );  
  23.     }finally{  
  24.         if(database!=null){  
  25.             try {  
  26.                 database.recycle();  
  27.             } catch (NotesException e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.         }  
  31.         if(session!=null){  
  32.             try {  
  33.                 session.recycle();  
  34.             } catch (NotesException e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.         }  
  38.     }  

带多个附件发送邮件

  1.  public void testSendMailWithAttachment(){  
  2.         String dominoServer = "192.168.0.2";  
  3.         String username = "cxx";  
  4.         Session session = null;  
  5.         Database database = null;  
  6.         try {  
  7.             //获取session  
  8.             session = NotesFactory.createSession(dominoServer, NotesFactory.createORB(), username, "1111");  
  9.             System.out.println("connect to mail server ok" );  
  10.             //得到数据库  
  11.             database = session.getDatabase(session.getServerName(), "mail//" + username + ".nsf"false);  
  12.             Document doc = database.createDocument();  
  13.               
  14.             //设置发件人  
  15.             doc.appendItemValue("Principal""cxx@testdomain.com");  
  16.             //设置主题  
  17.             doc.appendItemValue("Subject""我是主题");  
  18.             //设置收件人  
  19.             Vector toVect = new Vector();  
  20.             toVect.add("ggg@testdomain.com");  
  21.               
  22.             doc.appendItemValue("Form""Memo");  
  23.             RichTextItem rti = doc.createRichTextItem("Body");  
  24.             rti.addNewLine(2);  
  25.             rti.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null"D:/test.png""D:/test.png");  
  26.             rti.addNewLine(2);  
  27.             rti.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null"D:/test.txt""D:/test.txt");  
  28.               
  29.             //开始发送  
  30.             doc.send(true,toVect);  
  31.         } catch (Exception ex) {  
  32.             System.out.println("connect to mail server fail" );  
  33.         }finally{  
  34.             if(database!=null){  
  35.                 try {  
  36.                     database.recycle();  
  37.                 } catch (NotesException e) {  
  38.                     e.printStackTrace();  
  39.                 }  
  40.             }  
  41.             if(session!=null){  
  42.                 try {  
  43.                     session.recycle();  
  44.                 } catch (NotesException e) {  
  45.                     e.printStackTrace();  
  46.                 }  
  47.             }  
  48.         }  
  49.     } 

你可能感兴趣的:(java,database,null,session,邮件服务器,数据库,mail)