利用Notes的JAVA api发送邮件(代码片段)
最近要编写一个利用Notes自动发送邮件的程序,研究一天终于OK了``在这里跟大家分享代码
import lotus.domino.AgentBase;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.Session;
public abstract class EclipseAgentBase extends AgentBase {
public abstract void sendMail(String text);
public Session getSession() {
try {
return NotesFactory.createSession();
} catch (NotesException e) {
throw new RuntimeException("Unable to create session", e);
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import lotus.domino.NotesThread;
import lotus.domino.Session;
import lotus.domino.*;
public class MyAgent extends EclipseAgentBase {
private String sendname;
private String servername;
private String filepatch;
public MyAgent(){
try {
InputStream in = this.getClass().getResourceAsStream("/MailSystem.properties");
Properties props = new Properties();
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 (IOException e) {
e.printStackTrace();
}
}
public void sendMail(String text) {
try {
Session session = this.getSession();
System.out.println("Username: " + session.getUserName());
Database db=session.getDatabase(servername,filepatch,true);
Document domMail=db.createDocument();
domMail.appendItemValue("Form","Memo");
RichTextItem body=domMail.createRichTextItem("body");
body.appendText(text);
domMail.send(sendname);
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
}
}
MailSystem.properties
[email protected]
servername=Dominosrv/MVS
filepatch=mail/jiangmin.nsf
这两个类就是完整的代码,调用sendMail()就可以发送邮件了,光有代码是不能发送的,现在我们来说说环境的搭建,首先要安装Lotus Notes客护端(我这个程序不是运行在本地的,也就是说它不和Lotus Notes服务器在一起的,我们需要远程访问),然后我们需要两个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/