JAVA连接Domino服务器收发邮件代码示例

本文介绍如何使用JAVA连接Domino服务器进行邮件的收发操作,并提供了详细的代码示例,包括不带附件及带多个附件的邮件发送方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA连接Domino服务器收发邮件代码示例

今天在公司整理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", "cxx@testdomain.com"); //设置主题 doc.appendItemValue("Subject", "我是主题"); //设置收件人 Vector<String> toVect = new Vector<String>(); toVect.add("ggg@testdomain.com"); 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", "cxx@testdomain.com"); //设置主题 doc.appendItemValue("Subject", "我是主题"); //设置收件人 Vector<String> toVect = new Vector<String>(); toVect.add("ggg@testdomain.com"); 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一下。


帮朋友宣传:

新淘网依赖淘宝TOP平台的API,封装了多种酷炫组件,面向广大淘客提供免费建站推广功能,目前已经入住淘宝箱,正式对外运营,欢迎各位围观。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值