`
kakajw
  • 浏览: 263315 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

关于Java程序调用Lotus Notes邮件服务发送邮件的实现(二实践)

阅读更多

关于Java程序调用Lotus Notes邮件服务发送邮件的实现(二实践)

在第一部分中已找到该问题的解决思路,该部分主要叙述思路3(通过远程连接Domino服务器发送邮件)的实现过程,该过程主要分为两部分:Domino配置和程序代码。

 

一、Domino配置

通过阅读IBM官网的Lotus 官方文档架构师的文章(JavaDomino 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

如下所示:

 

 

2.BASIC选项卡 配置(配置“Fully qualified Internet host name”字段)

在服务器 Server 文档 Baiscs 选项卡的“Fully qualified Internet host name”字段中,确保该字段已设置为域名或IP地址,如下所示:

 

 

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);
		}
	}
}

 

 

 

  • 大小: 18.8 KB
  • 大小: 25.2 KB
  • 大小: 37.3 KB
分享到:
评论
2 楼 kakajw 2013-03-07  
原因可能是用户认证失败。
请参考本系列第三集:http://kakajw.iteye.com/blog/1768820 ,完成用户注册和配置

hbxflihua 写道
你好,我看了你的博客,写的很好。我在你的代码进行测试的时候,一直出现这样的异常:
NotesException: Notes error: You are not authorized to use the server
能说说解决的方法么?谢谢!
1 楼 hbxflihua 2013-03-01  
你好,我看了你的博客,写的很好。我在你的代码进行测试的时候,一直出现这样的异常:
NotesException: Notes error: You are not authorized to use the server
能说说解决的方法么?谢谢!

相关推荐

Global site tag (gtag.js) - Google Analytics