Qiluluwawa 2010-06-27
利用Notes的JAVAapi发送邮件(代码片段)
最近要编写一个利用Notes自动发送邮件的程序,研究一天终于OK了``在这里跟大家分享代码
importlotus.domino.AgentBase;
importlotus.domino.NotesException;
importlotus.domino.NotesFactory;
importlotus.domino.Session;
publicabstractclassEclipseAgentBaseextendsAgentBase{
publicabstractvoidsendMail(Stringtext);
publicSessiongetSession(){
try{
returnNotesFactory.createSession();
}catch(NotesExceptione){
thrownewRuntimeException("Unabletocreatesession",e);
}
}
}
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Properties;
importlotus.domino.NotesThread;
importlotus.domino.Session;
importlotus.domino.*;
publicclassMyAgentextendsEclipseAgentBase{
privateStringsendname;
privateStringservername;
privateStringfilepatch;
publicMyAgent(){
try{
InputStreamin=this.getClass().getResourceAsStream("/MailSystem.properties");
Propertiesprops=newProperties();
props.load(in);
this.sendname=props.getProperty("sendname");
this.servername=props.getProperty("servername");
this.filepatch=props.getProperty("filepatch");
//System.out.println(sendname);
//System.out.println(servername);
//System.out.println(filepatch);
}catch(IOExceptione){
e.printStackTrace();
}
}
publicvoidsendMail(Stringtext){
try{
Sessionsession=this.getSession();
System.out.println("Username:"+session.getUserName());
Databasedb=session.getDatabase(servername,filepatch,true);
DocumentdomMail=db.createDocument();
domMail.appendItemValue("Form","Memo");
RichTextItembody=domMail.createRichTextItem("body");
body.appendText(text);
domMail.send(sendname);
System.out.println("done");
}catch(Exceptione){
e.printStackTrace();
}
}
}
MailSystem.properties
servername=Dominosrv/MVS
filepatch=mail/jiangmin.nsf
这两个类就是完整的代码,调用sendMail()就可以发送邮件了,光有代码是不能发送的,现在我们来说说环境的搭建,首先要安装LotusNotes客护端(我这个程序不是运行在本地的,也就是说它不和LotusNotes服务器在一起的,我们需要远程访问),然后我们需要两个JAR包Notes.jar和NCSO.jar,把D:\lotus\notes加到patch路径下(也就是Notes的根目录),最后就是配置Notes客户端了,配置好了,一切OK```如果你关掉Notes客户端,那么该程序需要你输入密码,要想不用输入密码那么你就得一直看着客户端了(没办法).
http://www.ibm.com/developerworks/cn/lotus/ls-java_access_pt1/
api:
http://sourceforge.net/projects/domingo/files/