Monday, July 11, 2011

Send mail with Lotus Notes from Java

import java.util.ArrayList;
import java.util.List;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
/**
 * 
 * @author Toni Bünter, SRZXML
 * This class provides a services that connects to LN.
 */
public class LotusMailService {
 
    private static Log logger = LogFactory.getLog(LotusMailService.class);
 
    private String loginUrl = "http://intrawes-ite.zrh.swissre.com/webapp/sls/login";
    private String targetUrl = "http://intrawes-ite.zrh.swissre.com/webapp/idomzht1aa/DOMAPP/gurptest.nsf/fmMail?CreateDocument";
    private String flushUrl = "http://intrawes-ite.zrh.swissre.com/webapp/idomzht1aa/DOMAPP/gurptest.nsf/agMailSend?OpenAgent";
    private String userid;
    private String passwd;
 
    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }
 
    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }
 
    public void setFlushURL(String flushUrl) {
        this.flushUrl = flushUrl;
    }
 
    public void setUserid(String userid) {
        this.userid = userid;
    }
 
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
     
    /**
     * 
     * @param receiver mail address or receivers (could be 
     * SRZXLM, or toni_buenter@rcomext.com, or toni.buenter@ooit.com)
     * @param subject mail subject
     * @param body mail content (could contain a restricted set of HTML tags)
     * @throws Exception
     */
    public void sendMail(String receiver, String subject, String body)
            throws Exception {
        logger.info("INTRAWES_LOGIN_URL   : " + loginUrl);
        logger.info("LOTUS_MAILSERVER_URL : " + targetUrl);
        logger.info("LOTUS_MAILFLUSH_URL  : " + flushUrl);
        logger.debug("receiver            : " + receiver);
        logger.debug("subject             : " + subject);
        logger.debug("body                : " + body);
 
        DefaultHttpClient httpclient = new DefaultHttpClient();
 
        HttpPost httpPost = new HttpPost(loginUrl);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("userName", userid));
        nameValuePairs.add(new BasicNameValuePair("pin", passwd));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse rs = httpclient.execute(httpPost);
        Header statusHeader = rs.getFirstHeader("SLSStatus");
        httpPost.abort();
        if (statusHeader != null && "200".equals(statusHeader.getValue())) {
            HttpEntity ent = rs.getEntity();
            if (ent != null) {
                ent.consumeContent();
            }
            httpPost = new HttpPost(targetUrl);
            httpPost.addHeader("Content-Type",
                    "application/x-www-form-urlencoded");
            nameValuePairs = new ArrayList<NameValuePair>(3);
            nameValuePairs.add(new BasicNameValuePair("SendTo", receiver));
            nameValuePairs.add(new BasicNameValuePair("Subject", subject));
            nameValuePairs.add(new BasicNameValuePair("Body", body));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            rs = httpclient.execute(httpPost);
            logger.info("status line: " + rs.getStatusLine().getReasonPhrase());
            ent = rs.getEntity();
            if (ent != null) {
                String s = EntityUtils.toString(ent);
                logger.info("return message post: " + s);
                ent.consumeContent();
            }
            //
            // FLUSH
            //
            HttpGet httpget = new HttpGet(flushUrl);
            rs = httpclient.execute(httpget);
            ent = rs.getEntity();
            if (ent != null) {
                String s = EntityUtils.toString(ent);
                logger.info("return message flush: " + s);
                ent.consumeContent();
            }
            //
            //
            //
        } else {
            HttpEntity ent = rs.getEntity();
            logger.warn("Failed to logon");
            if (ent != null) {
                String s = EntityUtils.toString(ent);
                logger.info("return message: " + s);
                ent.consumeContent();
            }
        }
    }
 
 
}