用java傳送lotus郵件一個完整例子
阿新 • • 發佈:2019-01-27
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.NotesFactory;
import lotus.domino.Database;
import lotus.domino.EmbeddedObject;
import lotus.domino.RichTextItem;
import lotus.domino.Document;
import java.util.*;
/**
* 傳送lotus郵件,客戶是domino5.11版本,傳送郵件需要參考5.11的designer的JAVA庫對LOTUS資料庫的操作。
* 新增的附件必須放在DOMINO伺服器上,不能放在客戶端新增附件,不要問為什麼,這是事實。
* 例如:放在AS400 的em_01/test/test.tar,在程式使用相對路徑,"test/test.tar"相對於郵件的根目錄em_01.
*
* 在操作完後,要對DOMINO上的使用的資源釋放掉,一般只須釋放掉database和session. 語法:database.recycle(),session.recycle();
*
*
*
*
* @author songlb
*
*/
public class SendMailLotus {
private RichTextItem rti = null;
private Document memo = null;
private Session sNotes = null;
private Database dbMail = null;
public boolean attachFlag = false;
/** 日誌檔案 */
static org.apache.log4j.Logger log = org.apache.log4j.Logger
.getLogger(SendMailLotus.class);
public SendMailLotus() {
}
public void startSend(long userID, String[] to, String from,
String smtpServer, String popServer, String subject, int interval,
int priorityLevel, Date dateSent, String probeHost, String user,
String password, String returnState, String cawtoArgument,
int attachedSize, String sqlServer) throws AlarmException {
try {
//sNotes = (Session) NotesFactory.createSession(smtpServer, user, password);
String strIOR = NotesFactory.getIOR(smtpServer);
sNotes= NotesFactory.createSessionWithIOR(strIOR,user,password);
dbMail = (Database) sNotes.getDatabase(sNotes.getServerName(),
"mail//" + user + ".nsf", false);
{
if (dbMail == null) {
log.error("無法開啟使用者資料庫檔案" + user + ".nsf");
} else {
memo = (Document) dbMail.createDocument();
memo.appendItemValue("Form", "Memo");
subject = "good mail!";
memo.appendItemValue("Subject", subject);
rti = (RichTextItem) memo.createRichTextItem("Body");
rti.addNewLine(2);
rti.appendText("test mail!");
String attachFilePath ="附件在DOMINO伺服器上的位置";
java.util.Vector v = new java.util.Vector();
v.addElement(to[0]); // 收件人
memo.setEncryptOnSend(true);
memo.setSaveMessageOnSend(true);
memo.setSignOnSend(true);
memo.sign();
if (attachFlag && (!attachFilePath.trim().equals(""))) {
rti.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null,
attachFilePath, attachFilePath); // 新增附件
}
memo.setSaveMessageOnSend(true);
memo.send(true, v);
}
}
StringBuffer tmp = new StringBuffer();
for (int j = 0; j < to.length; j++) {
tmp.append(" ");
tmp.append(to[j]);
}
java.util.Date endDate = new Date();
log.info("傳送郵件成功!");
MailDatabase.insertMailSent(userID, sqlServer, from,
tmp.toString(), smtpServer, popServer, subject, interval,
priorityLevel, dateSent, endDate, probeHost, attachedSize,
returnState, null, null);
} catch (NotesException e) {
log.error("傳送LOTUS郵件失敗:" + ((lotus.domino.NotesException) e).text);
} catch (Exception ex) {
log.error("傳送LOTUS郵件失敗:" + ex);
} finally {
try {
if(sNotes!=null){
if(dbMail!=null){
dbMail.recycle();
}
sNotes.recycle(); // 一定要關閉,否則會使SERVER崩潰
dbMail=null;
sNotes=null;
//log.info("===========lotus version:"+ sNotes.getNotesVersion());
}
} catch (NotesException ex1) {
log.error("關閉SESSION出錯!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SendMailLotus send = new SendMailLotus();
ProxyHandler.test();
try {
String[] p = new String[1];
p[0] = " [email protected]";
for (int i = 0; i < 3; i++) {
send.startSend(1, p, "mailtest", "10.68.100.233",
"10.68.100.233", "why i's check it!", 20, 1,
new Date(), "127.0.0.1", "mailtest", "password", "0",
"0", 0, "127.0.0.1");
}
} catch (Exception ex) {
ex.printStackTrace();
}
log.info("訪問結束!");
}
}
import lotus.domino.Session;
import lotus.domino.NotesFactory;
import lotus.domino.Database;
import lotus.domino.EmbeddedObject;
import lotus.domino.RichTextItem;
import lotus.domino.Document;
import java.util.*;
/**
* 傳送lotus郵件,客戶是domino5.11版本,傳送郵件需要參考5.11的designer的JAVA庫對LOTUS資料庫的操作。
* 新增的附件必須放在DOMINO伺服器上,不能放在客戶端新增附件,不要問為什麼,這是事實。
* 例如:放在AS400 的em_01/test/test.tar,在程式使用相對路徑,"test/test.tar"相對於郵件的根目錄em_01.
*
* 在操作完後,要對DOMINO上的使用的資源釋放掉,一般只須釋放掉database和session. 語法:database.recycle(),session.recycle();
*
*
*
*
* @author songlb
*
*/
public class SendMailLotus {
private RichTextItem rti = null;
private Document memo = null;
private Session sNotes = null;
private Database dbMail = null;
public boolean attachFlag = false;
/** 日誌檔案 */
static org.apache.log4j.Logger log = org.apache.log4j.Logger
.getLogger(SendMailLotus.class);
public SendMailLotus() {
}
public void startSend(long userID, String[] to, String from,
String smtpServer, String popServer, String subject, int interval,
int priorityLevel, Date dateSent, String probeHost, String user,
String password, String returnState, String cawtoArgument,
int attachedSize, String sqlServer) throws AlarmException {
try {
//sNotes = (Session) NotesFactory.createSession(smtpServer, user, password);
String strIOR = NotesFactory.getIOR(smtpServer);
sNotes= NotesFactory.createSessionWithIOR(strIOR,user,password);
dbMail = (Database) sNotes.getDatabase(sNotes.getServerName(),
"mail//" + user + ".nsf", false);
{
if (dbMail == null) {
log.error("無法開啟使用者資料庫檔案" + user + ".nsf");
} else {
memo = (Document) dbMail.createDocument();
memo.appendItemValue("Form", "Memo");
subject = "good mail!";
memo.appendItemValue("Subject", subject);
rti = (RichTextItem) memo.createRichTextItem("Body");
rti.addNewLine(2);
rti.appendText("test mail!");
String attachFilePath ="附件在DOMINO伺服器上的位置";
java.util.Vector v = new java.util.Vector();
v.addElement(to[0]); // 收件人
memo.setEncryptOnSend(true);
memo.setSaveMessageOnSend(true);
memo.setSignOnSend(true);
memo.sign();
if (attachFlag && (!attachFilePath.trim().equals(""))) {
rti.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null,
attachFilePath, attachFilePath); // 新增附件
}
memo.setSaveMessageOnSend(true);
memo.send(true, v);
}
}
StringBuffer tmp = new StringBuffer();
for (int j = 0; j < to.length; j++) {
tmp.append(" ");
tmp.append(to[j]);
}
java.util.Date endDate = new Date();
log.info("傳送郵件成功!");
MailDatabase.insertMailSent(userID, sqlServer, from,
tmp.toString(), smtpServer, popServer, subject, interval,
priorityLevel, dateSent, endDate, probeHost, attachedSize,
returnState, null, null);
} catch (NotesException e) {
log.error("傳送LOTUS郵件失敗:" + ((lotus.domino.NotesException) e).text);
} catch (Exception ex) {
log.error("傳送LOTUS郵件失敗:" + ex);
} finally {
try {
if(sNotes!=null){
if(dbMail!=null){
dbMail.recycle();
}
sNotes.recycle(); // 一定要關閉,否則會使SERVER崩潰
dbMail=null;
sNotes=null;
//log.info("===========lotus version:"+ sNotes.getNotesVersion());
}
} catch (NotesException ex1) {
log.error("關閉SESSION出錯!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SendMailLotus send = new SendMailLotus();
ProxyHandler.test();
try {
String[] p = new String[1];
p[0] = "
for (int i = 0; i < 3; i++) {
send.startSend(1, p, "mailtest", "10.68.100.233",
"10.68.100.233", "why i's check it!", 20, 1,
new Date(), "127.0.0.1", "mailtest", "password", "0",
"0", 0, "127.0.0.1");
}
} catch (Exception ex) {
ex.printStackTrace();
}
log.info("訪問結束!");
}
}