首页  ·  知识 ·  编程语言
Java程序调用LotusNotes邮件服务发送邮件的实现
kakajw  CIOZJ  Java  编辑:dezai   图片来源:网络
在第一部分中已找到该问题的解决思路,该部分主要叙述思路3(通过远程连接Domino服务器发送邮件)的实现过程,该过程主要分为两部分:Domino配置和程序代码。

一、Domino配置

通过阅读IBM官网的Lotus 官方文档架构师的文章(Java对Domino Objects的访问:

http://www.ibm.com/developerworks/cn/lotus/ls-java_access_pt1/index.html),你会发现,要通过远程连接的方式发送邮件,需要Domino服务器开启 DIIOP 任务和端口以及其他相关配置正确,配置项如下:

A.确保已开启 DIIOP 任务

要在服务器上启动 HTTP 和 DIIOP 任务,需要确保这些任务在 Notes.ini 文件 ServerTasks 变量的任务列表中; 

如果正确配置了 Server 文档该文件,Notes.ini 文件应该包含类似于下面的行:ServerTasks=Update,Replica,Router,AMgr, AdminP,CalConn,Sched,DIIOP,HTTP,LDAP

 

也可从运行的服务器,在控制台中输入下列命令来临时加载任务:

启动 DIIOP 任务:load diiop

但是在Domino服务器重启后,以命令加载DIIOP任务的方式无效

 

B.服务器配置文件的配置

1.访问控制(指定DIIOP服务端口,并设置为以用户名密码方式访问DIIOP服务)

在 Server 文档中,转至 Ports 选项卡、Internet Ports 选项卡和 DIIOP 选项卡。

设置DIIOP 的端口号,默认为 63148

设置Name&password字段为yes;

如下所示:

 

 

“Fully qualified Internet host name”字段是 createSession 主机的(第一个)参数。

也可以使用服务器的 IP 地址。例如,如果 myhost.east.acme.com 的 IP 地址是 9.95.73.30,那么下列任何一个调用都有效:

Session s = NotesFactory.createSession("myhost.east.acme.com:63148")

或者 Session s = NotesFactory.createSession("9.95.73.30:63148")

 

3. TCP/IP的网络地址项配置

打开“当前服务器文档”–>“port”–>“Notes Network port”–>“TCPIP”–>“Net Address”项,

将 Net Address的值设定为服务器主机的IP地址。

 

 

二、程序代码

 

  1.定义邮件实体类MailContent

package com.jhh;  

  
public class MailContent {  
    private String form;  
    private String subject;  
    private String body;  
      
    MailContent(){        
    }  
      
    MailContent(String body){  
        this(null,null,body);  
    }  
      
    MailContent(String subject,String body){  
        this(null,subject,body);  
    }  
      
    MailContent(String form,String subject,String body){  
        this.form = form;  
        this.subject = subject;  
        this.body = body;  
    }     
      
      
    public String getForm() {  
        return form;  
    }  
    public void setForm(String form) {  
        this.form = form;  
    }  
    public String getSubject() {  
        return subject;  
    }  
    public void setSubject(String subject) {  
        this.subject = subject;  
    }  
    public String getBody() {  
        return body;  
    }  
    public void setBody(String body) {  
        this.body = body;  
    }  
      
      
}  
 2.核心类NotesMailManager
 
package com.jhh;  
  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Properties;  
import java.util.Vector;  
import lotus.domino.Database;  
import lotus.domino.Document;  
import lotus.domino.NotesException;  
import lotus.domino.NotesFactory;  
import lotus.domino.Session;  
  
public class NotesMailManager {   
    private String recipients;  
    private String dominoServerName;  
    private String userFilePath;  
    private String host;  
    private String userName;  
    private String password;      
  
      
    public Session getNotesSession(){  
        Session session = null;       
  
        try {  
            session = NotesFactory.createSession(host,userName,password);             
        } catch (NotesException e) {  
          System.out.println("Error happens when creating session using DIIOP port.");    
          e.printStackTrace();  
          return null;  
        }  
        return session;  
    }  
      
    public Session getNotesSession(String host,String userName,String passwd){  
        this.setHost(host);  
        this.setUserName(userName);  
        this.setPassword(passwd);  
        return getNotesSession();  
    }  
      
    public void sendMail(Session session,MailContent mailContent){  
        if (session == null){  
            System.out.println("Fail to send mail for: session is null!");  
            return;  
        }         
        boolean isloaded = loadProperty();  
        Database database = null;  
        Document mailDom = null;          
        if (isloaded){                
            try {  
                System.out.println("User: " + session.getUserName());  
                database = session.getDatabase(dominoServerName,userFilePath,true);               
                System.out.println("Database URL: "+database.getURL());  
                mailDom = database.createDocument();     
                mailDom.replaceItemValue("Form",mailContent.getForm());     
                mailDom.replaceItemValue("Subject",mailContent.getSubject());  
                mailDom.replaceItemValue("Body",mailContent.getBody());  
                Vector<String>  recipientsVector = new Vector<String>();  
                String [] recipientArray = recipients.split(",");  
                System.out.print("send to: ");  
                for(String rept:recipientArray){  
                    recipientsVector.add(rept);  
                    System.out.print(rept+"  ");  
                }  
                mailDom.save();  
                mailDom.send(recipientsVector);              
            } catch (NotesException e) {  
                System.out.println("Fail to send mail for NotesException!");  
                e.printStackTrace();  
                return;  
            }finally{  
                try {  
                    if (mailDom != null){  
                        mailDom.recycle();  
                        mailDom = null;  
                    }  
                    if (database != null){  
                        database.recycle();  
                        database = null;  
                    }  
                    if (session != null ){  
                        session.recycle();  
                        session = null;  
                    }  
                }catch (NotesException e1){  
                      
                }  
            }  
          System.out.println("\n Done! The mail has been successfully sent.");    
        }             
          
    }  
      
    public boolean loadProperty(){  
        InputStream in = this.getClass().getResourceAsStream("/Mail.properties");  
        Properties props = new Properties();  
         try {  
            props.load(in);  
        } catch (IOException e) {  
            try {  
                if (in != null){  
                    in.close();  
                }  
                in = null;  
            } catch (IOException e1) {        
                e1.printStackTrace();  
            }  
            e.printStackTrace();  
            return false;  
        }  
        dominoServerName = props.getProperty("dominoServerName");  
        userFilePath = props.getProperty("userFilePath");  
        recipients = props.getProperty("recipientsAdress");  
        host = props.getProperty("host");  
        userName = props.getProperty("userName");  
        password = props.getProperty("password");         
        return true;  
    }  
  
    public String getRecipients() {  
        return recipients;  
    }  
  
    public void setRecipients(String recipients) {  
        this.recipients = recipients;  
    }  
  
    public String getDominoServerName() {  
        return dominoServerName;  
    }  
  
    public void setDominoServerName(String dominoServerName) {  
        this.dominoServerName = dominoServerName;  
    }  
  
    public String getUserFilePath() {  
        return userFilePath;  
    }  
  
    public void setUserFilePath(String userFilePath) {  
        this.userFilePath = userFilePath;  
    }  
  
    public String getHost() {  
        return host;  
    }  
  
    public void setHost(String host) {  
        this.host = host;  
    }  
  
    public String getUserName() {  
        return userName;  
    }  
  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
  
    public String getPassword() {  
        return password;  
    }  
  
    public void setPassword(String password) {  
        this.password = password;  
    }         
      
      
 }  
3.程序测试入口类MailTest
package com.jhh;  
  
import java.util.Date;  
import lotus.domino.Session;  
  
public class MailTest {  
  
    public static void main(String[] args) {  
        NotesMailManager manager = new NotesMailManager();  
        boolean isloaded = manager.loadProperty();  
        if (isloaded){  
            Session session = manager.getNotesSession();  
            MailContent mc = new MailContent("System mail--test","for mail test ! \n"+(new Date()).toString());  
            manager.sendMail(session,mc);  
        }  
    }  
}  
本文作者:kakajw 来源:CIOZJ
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的