Google其解法大多是Override NotesMain method,但欠缺使用Java standalone的例子,還得參考VB call法才取得更進階實作方式。目前踹出的solution不包括對外使用STMP,只發給Notes裡的成員。
第一步,要寫個抽象類別繼承AgentBase,我只是奇怪為何不能只繼承AgentBase就好了?
@ImportResource(value={"classpath:resources.xml"}) public abstract class AbstractAgentBase extends AgentBase { private @Value("#{mailProperties.server_url}") String serverUrl; private @Value("#{mailProperties.server_id}") String serverId; private @Value("#{mailProperties.server_pwd}") String serverPwd; private Session pSession = null; public Session getSession() { try { if (null == this.pSession) { String ior = NotesFactory.getIOR(this.serverUrl); this.pSession = NotesFactory.createSessionWithIOR(ior, this.serverId, this.serverPwd); // this.pSession = NotesFactory.createSession(this.serverUrl, this.serverId, this.serverPwd); } return this.pSession; } catch (NotesException e) { throw new RuntimeException("Unable to create session", e); } } } |
取得Notes的Session有二,一個是上例使用IOR取得,另一個是註解的方式。第二步,再繼承自己寫的抽象類別:
@ImportResource(value={"classpath:resources.xml"}) public class NotesMailServiceImpl extends AbstractAgentBase { private @Value("#{mailProperties.db_name}") String dbName; public boolean sendMail(String oggId, String mailContext) { try { Session session = this.getSession(); System.out.println("Username: " + session.getUserName()); Database db=session.getDatabase(session.getServerName(),dbName, true); Document domMail=db.createDocument(); domMail.appendItemValue("Form", "Memo"); domMail.appendItemValue("Subject", "Warnning"); Vector<String> vecRecipient = new Vector<String>(); vecRecipient.add("Jemmy"); domMail.appendItemValue("sendTo", vecRecipient); Vector<String> vecCopyTo = new Vector<String>(); vecCopyTo.add("Jemmy"); domMail.appendItemValue("copyTo", vecCopyTo); RichTextItem body=domMail.createRichTextItem("body"); body.appendText(mailContext); domMail.send(); return true; } catch (Exception e) { logger.warn("sendMail", e); return false; } } } |
有趣的問題來了,紅字部份是Session透過繼承自己寫的抽象類別取得,卻無法直接使用NotesFactory.createSession方式取得,真厚工。
目前了解的Notes物件體系就是Session => Database => Document => Item的樣子,所以Mail也是一個Document。而Notes for Java的API相較於VB並不友善,Mail Document的send method就是發Mail,也可以帶參數,String或Vector類別,即Notes User的名字,若是一次發送多人,則Vector裡放多人的Notes User名。但若是需要副本抄送(cc)及密件副本(bcc)怎麼處理?在Java API並沒有像VB API一樣有相應的method,它還是透過appendItemValue的放Hash value,使用Hash最大的缺點就是Key name要找對,以下是寄Mail的正確Key name(含大小寫):
sendTo:收件者
copyTo:副本抄送
blindCopyTo:密件副本