Domino 生成邮件到草稿箱

相关架包下载地址:


http://download.csdn.net/detail/qm4050/5531483


代码如下:

import java.io.File;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.Item;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.RichTextItem;
import lotus.domino.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MailPDF {
	private static final Logger log = LoggerFactory.getLogger(MailPDF.class);
	private Session session;
	private Database mailDb;
	private static MailPDF instance = new MailPDF();

	private MailPDF() {

	}

	/**
	 * 构造器
	 * 
	 * @param server
	 *            Domino Server IP Address or DNS
	 * @param user
	 *            who connect to Domino Server
	 * @param password
	 *            who's password
	 * @param senderMailFile
	 *            the mail file path relatived to <Domino>\Data directory.
	 * @return the class instance.
	 * @throws NotesException
	 */
	public static MailPDF create(String server, String user, String password, String senderMailFile)
			throws NotesException {
		if (instance.session == null || !instance.session.isValid()) {
			instance.session = NotesFactory.createSession(server, user, password);
		}
		instance.mailDb = instance.session.getDatabase(null, senderMailFile);

		return instance;
	}

	/**
	 * 在特定用户处创建邮件
	 * 
	 * @param sender
	 *            发件人
	 * @param recipients
	 *            收件人
	 * @param cc
	 *            抄送
	 * @param bcc
	 *            密送
	 * @param body
	 *            正文内容
	 * @param pdfPath
	 *            PDF文件全路径
	 * @return 是否创建成功
	 * @throws NotesException
	 */
	public boolean createDraftMail(String sender, String[] recipients, String[] cc, String[] bcc, String subject,
			String[] body, String pdfPath) throws RuntimeException, NotesException {
		if (session == null || !session.isValid()) {
			throw new RuntimeException("No Notes Session");
		}
		if (sender == null || "".equals(sender)) {
			throw new RuntimeException("Sender is Empty");
		}
		if (subject == null) {
			throw new RuntimeException("Subject is Empty");
		}
		if (mailDb == null) {
			throw new RuntimeException("Mail Database Open Failed.");
		}
		if (!mailDb.isOpen())
			mailDb.open();

		if (pdfPath == null || "".equals(pdfPath)) {
			throw new RuntimeException("PDF file Not Found");
		}

		Document doc = mailDb.createDocument();
		Item item;
		doc.replaceItemValue("Form", "Memo");
		doc.replaceItemValue("Subject", subject);
		item = doc.replaceItemValue("EnterSendTo","");
		for (String recipient : recipients) {
			item.appendToTextList(recipient);
		}
		if (cc != null) {
			item = doc.replaceItemValue("EnterCopyTo","");
			for (String c : cc) {
				item.appendToTextList(c);
			}
		}
		if (bcc != null) {
			item = doc.replaceItemValue("EnterBlindCopyTo","");
			for (String bc : bcc) {
				item.appendToTextList(bc);
			}
		}
		RichTextItem rtItem = doc.createRichTextItem("Body");
		if (body != null) {
			for (String b : body) {
				rtItem.appendText(b);
				rtItem.addNewLine();
			}
		}
		String pdfName = pdfPath.substring(pdfPath.lastIndexOf(File.separator) + File.separator.length());
		rtItem.embedObject(1454, "", pdfPath, pdfName);
		doc.computeWithForm(true, false);
		doc.save();
		return true;
	}

	public void close() {
		try {
			this.session.recycle();
		} catch (NotesException e) {
			log.error("", e);
		}
	}
	
	
	public static void main(String[] args) {
		try {
			MailPDF mp = MailPDF.create("localhost", "username", "password", "mail/username.nsf");
			mp.createDraftMail("username@demo.com", 
					new String[] { "demo/demo", "demo@demo.com" }, 
					new String[] {"demo/demo", "demo@demo.com" }, 
					new String[] { "demo/demo", "demo@demo.com" }, 
					"test",
					new String[] { "test", "test" }, "D:/test.pdf");
		} catch (NotesException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.text);
		}
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值