现在的位置: 首页 > 综合 > 正文

用java接收lotus邮件一个完整例子

2013年08月31日 ⁄ 综合 ⁄ 共 9150字 ⁄ 字号 评论关闭
import java.util.Date;
import java.util.Vector;

import org.apache.log4j.Logger;
import java.util.*;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.DocumentCollection;
import lotus.domino.EmbeddedObject;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.RichTextItem;
import lotus.domino.RichTextStyle;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.ViewColumn;
import lotus.domino.ViewEntryCollection;

public class ReceiverMailLotus {
    private int count = 0;

    private String sqlServer = "";

    private String subject = "", body = "", probeHost = "", from = "", to = "",
            replyTo = "";

    private Session sNotes = null;

    static Logger log = Logger.getLogger(ReceiverMailLotus.class);

    /**
     * 测试一个数据库中所有的视图名称,5.01与6.5有很大的不同
     *
     * @param db
     *            Database
     */
    public void testViews(Database db) {
        Vector views = null;
        try {
            views = db.getViews();
            for (int i = 0; i < views.size(); i++) {
                View view = (View) views.elementAt(i);

                System.out.println(view.getName() + " with "
                        + view.getColumnCount() + " columns in /""
                        + view.getParent().getTitle() + "/"");
                ViewEntryCollection entries = view.getAllEntries();
                System.out.println("/tNumber of document entries = "
                        + entries.getCount());
                Vector columnNames = view.getColumnNames();
                Vector columns = view.getColumns();
                for (int j = 0; j < columns.size(); j++) {
                    String columnName = ((String) columnNames.elementAt(j))
                            .trim();
                    if (columnName.length() == 0) {
                        columnName = "No Name";
                    }
                    ViewColumn column = (ViewColumn) columns.elementAt(j);
                    System.out.println("/t[" + columnName + "] has width of "
                            + column.getWidth());
                }
                Vector aliases = view.getAliases();
                for (int j = 0; j < aliases.size(); j++) {
                    System.out.println("/tAlias /"" + aliases.elementAt(j)
                            + "/"");
                }
                System.out.println("/tUniversal ID: " + view.getUniversalID());
                System.out.println("/tCreated: " + view.getCreated());
                System.out
                        .println("/tLast modified: " + view.getLastModified());
                System.out.println("/tDefault view? " + view.isDefaultView());
                System.out.println("/tAuto update? " + view.isAutoUpdate());
                System.out.println("/tFolder? " + view.isFolder());
                System.out.println("/tCalendar? " + view.isCalendar());
                System.out.println("/tConflict? " + view.isConflict());
                System.out.println("/tModified? " + view.isModified());
                System.out.println("/tProtect readers? "
                        + view.isProtectReaders());
                System.out.println("/tCategorized? " + view.isCategorized());
                System.out.println("/tHierarchical? " + view.isHierarchical());
                Vector readers = view.getReaders();
                for (int j = 0; j < readers.size(); j++) {
                    System.out.println("/tReader /"" + readers.elementAt(j)
                            + "/"");
                }
                switch (view.getBackgroundColor()) {
                case RichTextStyle.COLOR_BLACK:
                    System.out.println("/tBackground color is black");
                    break;
                case RichTextStyle.COLOR_WHITE:
                    System.out.println("/tBackground color is white");
                    break;
                default:
                    System.out.println("/tBackground color not black or white");
                }
                System.out.println("/tHeaderLines = " + view.getHeaderLines());
                System.out.println("/tRowLines = " + view.getRowLines());
                String spacing = null;
                switch (view.getSpacing()) {
                case View.SPACING_DOUBLE:
                    spacing = "double";
                    break;
                case View.SPACING_ONE_POINT_25:
                    spacing = "1.25";
                    break;
                case View.SPACING_ONE_POINT_50:
                    spacing = "1.5";
                    break;
                case View.SPACING_ONE_POINT_75:
                    spacing = "1.75";
                    break;
                case View.SPACING_SINGLE:
                    spacing = "single";
                    break;
                default:
                    spacing = "unknown";
                }
                System.out.println("/tSpacing = " + spacing);
                System.out.println("/tTopLevelEntryCount = "
                        + view.getTopLevelEntryCount());
            }

        } catch (NotesException ex) {
        }

    }

    /**
     * 清空发件箱的邮件
     *
     * @param dbMail
     *            Database
     */
    public void clearSendBox(Database dbMail) {

        try {
            View view = dbMail.getView("($Sent)");
            Document doc = view.getFirstDocument();
            while (doc != null) {
                doc.remove(true);
                view.refresh();
                doc = view.getFirstDocument();
            }
        } catch (NotesException ex) {
            log.error("清空发件箱失败:" + ex.text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 接收邮件
     *
     * @param protocol
     * @param mailHost
     * @param user
     * @param passwd
     * @param maxMailCount
     * @param isAutoDelete
     * @param firstRUN
     */
    public void receive(long id, String protocol, String mailHost, String user,
            String passwd, int maxMailCount, boolean isAutoDelete)
            throws AlarmException {

        Document doc = null;
        DocumentCollection dcAll = null;
        Database dbMail = null;

        String receivedState = "";
        Date sentDate = new Date();
        Date receivedDate = sentDate;
        int size = 0, priority = 0;

        try {
            probeHost = InetAddress.getLocalHost().getHostName();

            String strIOR = NotesFactory.getIOR(mailHost);
            sNotes= NotesFactory.createSessionWithIOR(strIOR,user,passwd); 

            {
                dbMail = (Database) sNotes.getDatabase(sNotes.getServerName(),
                        "mail//" + user + ".nsf", false);

                View view = dbMail.getView("($Inbox)"); // 取得收件箱的视图

                // testViews(dbMail);//测试全部VIEW的名称
                clearSendBox(dbMail); // 清空发件箱

                doc = (Document) view.getFirstDocument();
                //log.info("come in 5");
       
                // 遍历所有的新邮件
                while (doc != null) {
                    //log.info("come in 6");
                    try{
                    subject = doc.getItemValueString("Subject").trim();
                    //log.info(subject);
                    body = doc.getItemValueString("Body");

                    RichTextItem bodyT = (RichTextItem) doc
                            .getFirstItem("Body");
;
                    Vector v = null;
                    if (bodyT != null) {
                        v = bodyT.getEmbeddedObjects();

                    }
                    if (v != null && v.size() > 0) {

                        Enumeration e = v.elements();
                        while (e.hasMoreElements()) {
                            EmbeddedObject eo = (EmbeddedObject) e
                                    .nextElement();

                            String type = null;
                            // log.info("eo.getType:" + eo.getType());
                            switch (eo.getType()) {
                            case EmbeddedObject.EMBED_ATTACHMENT:
                                type = "file attachment";
                                break;
                            case EmbeddedObject.EMBED_OBJECT:
                                type = "embedded object";
                                break;
                            case EmbeddedObject.EMBED_OBJECTLINK:
                                type = "object link";
                            }

                            if (type.equals("file attachment")) {
                                size = eo.getFileSize(); // 得到附件的大小
                                log.info("attachSize:" + size);
                            }
                        }
                    }

                    size = doc.getSize();// 默认把一个文档的全部容量
                    //log.info("total size:"+size);
                    Vector i = doc.getItemValue("Received");

                    from = doc.getItemValueString("From");
                    log.info("发信人:" + from);

                    to = doc.getItemValueString("Delivered-To");
                    if (to == null) {
                        to = "";
                    }
                    replyTo = doc.getItemValueString("Return-Path");

                    priority = doc.getItemValueInteger("X-Priority");

                    doc.remove(true); // 这种删除,在5.11中是在收件箱中删除了,不会放到回收站,但是在6.5是放到回收站中去了。不过,5.01模拟收发邮件程序在发送一封邮件后,“发件箱”
                    // 会留有发送记录,所以,不要把发件箱的记录清除掉。
                    view.refresh(); // 视图必须刷新,否则会出错,DOMINO帮助有问题。
                    doc = (Document) view.getFirstDocument();
                }
            }
        } catch (NotesException e) {
            log.error("接收LOTUS邮件出错:可能用户名的密码输入错误有误!");

        }

        catch (RuntimeException re) {
            log.error("接收LOTUS邮件出错:" + re.getMessage());

        } catch (Exception ex) {
            ex.printStackTrace();

        } finally {
            if (sNotes != null) {
                try {
                    if (dbMail != null) {
                        dbMail.recycle();
                    }

                    sNotes.recycle();

                    dbMail=null;
                    sNotes = null;
                } catch (NotesException ex1) {
                    log.error("关闭SESSION出错!");
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }

    }

    public int getMailReceived() {
        return count;
    }

    public static void main(String[] args) {
        ReceiverMailLotus receive = new ReceiverMailLotus("127.0.0.1");

        try {
            receive.receive(111, "lotus", "192.168.0.10", "mailtest",
                    "password", 100, true);
        } catch (AlarmException ex) {
            ex.printStackTrace();
        }
        log.info("访问结束!");

    }
}

抱歉!评论已关闭.