⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messageprocessor.java

📁 邮件服务器系统 支持SMTP POP3 是著名的Apache写 有一定的参考价值
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    }            /**     * Method isLocalRecipient.     * @param recipient     * @return boolean     */    protected boolean isLocalRecipient(MailAddress recipient)    {        return isLocalUser(recipient) && isLocalServer(recipient);    }        /**     * Method isLocalServer.     * @param recipient     * @return boolean     */    protected boolean isLocalServer(MailAddress recipient)    {        return getServer().isLocalServer(recipient.getHost());    }        /**     * Method isLocalUser.     * @param recipient     * @return boolean     */    protected boolean isLocalUser(MailAddress recipient)    {        return getLocalUsers().containsCaseInsensitive(recipient.getUser());    }               /**     * Method isBlacklistedRecipient.     * @param recipient     * @return boolean     */    protected boolean isBlacklistedRecipient(MailAddress recipient)    {        return getBlacklist().contains(recipient);    }           /**     * Check if this mail has been bouncing by counting the X-fetched-from      * headers for this task     *      * @return boolean     */    protected boolean isBouncing() throws MessagingException    {        Enumeration enum =            getMessageIn().getMatchingHeaderLines(                new String[] { "X-fetched-from" });        int count = 0;        while (enum.hasMoreElements())        {            String header = (String) enum.nextElement();            if (header.equals(getFetchTaskName()))                count++;        }        return count >= 3;    }        /**     * Method sendMail.     * @param mail     * @throws MessagingException     */    protected void sendMail(Mail mail) throws MessagingException    {        // send the mail        getServer().sendMail(mail);        // Update the flags of the received message        if (!isLeave())            setMessageDeleted();        if (isMarkSeen())            setMessageSeen();        // Log the status        StringBuffer messageBuffer =            new StringBuffer("Spooled message to recipients: ");        Iterator recipientIterator = mail.getRecipients().iterator();        while (recipientIterator.hasNext())        {            messageBuffer.append(recipientIterator.next());            messageBuffer.append(' ');        }        messageBuffer.append('.');        logStatusInfo(messageBuffer.toString());    }       /**     * Method getEnvelopeRecipient answers the recipient if found else null.     *      * Try and parse the "for" parameter from a Received header     * Maybe not the most accurate parsing in the world but it should do     * I opted not to use ORO (maybe I should have)     *      * @param msg     * @return String     */    protected String getEnvelopeRecipient(MimeMessage msg) throws MessagingException    {        try        {            Enumeration enum =                msg.getMatchingHeaderLines(new String[] { "Received" });            while (enum.hasMoreElements())            {                String received = (String) enum.nextElement();                int nextSearchAt = 0;                int i = 0;                int start = 0;                int end = 0;                boolean hasBracket = false;                boolean usableAddress = false;                while (!usableAddress && (i != -1))                {                    hasBracket = false;                    i = received.indexOf("for ", nextSearchAt);                    if (i > 0)                    {                        start = i + 4;                        end = 0;                        nextSearchAt = start;                        for (int c = start; c < received.length(); c++)                        {                            char ch = received.charAt(c);                            switch (ch)                            {                                case '<' :                                    hasBracket = true;                                    continue;                                case '@' :                                    usableAddress = true;                                    continue;                                case ' ' :                                    end = c;                                    break;                                case ';' :                                    end = c;                                    break;                            }                            if (end > 0)                                break;                        }                    }                }                if (usableAddress)                {                    // lets try and grab the email address                    String mailFor = received.substring(start, end);                    // strip the <> around the address if there are any                    if (mailFor.startsWith("<") && mailFor.endsWith(">"))                        mailFor = mailFor.substring(1, (mailFor.length() - 1));                    return mailFor;                }            }        }        catch (MessagingException me)        {            logStatusWarn("No Received headers found.");        }        return null;    }        /**     * Method getIntendedRecipient answers the sole intended recipient else null.     *      * @return MailAddress     * @throws MessagingException     */    protected MailAddress getIntendedRecipient() throws MessagingException    {        // If the original recipient should be ignored, answer the         // hard-coded recipient        if (isIgnoreRecipientHeader())        {            StringBuffer messageBuffer =                new StringBuffer("Ignoring recipient header. Using configured recipient as new envelope recipient: ");            messageBuffer.append(getRecipient());            messageBuffer.append('.');                        logStatusInfo(messageBuffer.toString());            return getRecipient();        }        // If we can determine who the message was received for, answer        // the target recipient        String targetRecipient = getEnvelopeRecipient(getMessageIn());        if (targetRecipient != null)        {            MailAddress recipient = new MailAddress(targetRecipient);            StringBuffer messageBuffer =                new StringBuffer("Using original envelope recipient as new envelope recipient: ");            messageBuffer.append(recipient);            messageBuffer.append('.');                          logStatusInfo(messageBuffer.toString());            return recipient;        }        // If we can determine the intended recipient from all of the recipients,        // answer the intended recipient. This requires that there is exactly one        // recipient answered by getAllRecipients(), which examines the TO: CC: and        // BCC: headers        Address[] allRecipients = getMessageIn().getAllRecipients();        if (allRecipients.length == 1)        {            MailAddress recipient =                new MailAddress((InternetAddress) allRecipients[0]);            StringBuffer messageBuffer =                new StringBuffer("Using sole recipient header address as new envelope recipient: ");            messageBuffer.append(recipient);            messageBuffer.append('.');                          logStatusInfo(messageBuffer.toString());            return recipient;        }        return null;    }               /**     * Returns the messageIn.     * @return MimeMessage     */    protected MimeMessage getMessageIn()    {        return fieldMessageIn;    }    /**     * Sets the messageIn.     * @param messageIn The messageIn to set     */    protected void setMessageIn(MimeMessage messageIn)    {        fieldMessageIn = messageIn;    }    /**     * Returns the localRecipient.     * @return boolean     */    protected boolean isRemoteRecipient()    {        return fieldRemoteRecipient;    }        /**     * Returns <code>boolean</code> indicating if the message to be delivered     * was unprocessed in a previous delivery attempt.     * @return boolean     */    protected boolean isPreviouslyUnprocessed()    {        return true;    }        /**     * Log the status of the current message as INFO.     * @param detailMsg     */    protected void logStatusInfo(String detailMsg) throws MessagingException    {        getLogger().info(getStatusReport(detailMsg).toString());    }    /**     * Log the status the current message as WARN.     * @param detailMsg     */    protected void logStatusWarn(String detailMsg) throws MessagingException    {        getLogger().warn(getStatusReport(detailMsg).toString());    }        /**     * Log the status the current message as ERROR.     * @param detailMsg     */    protected void logStatusError(String detailMsg) throws MessagingException    {        getLogger().error(getStatusReport(detailMsg).toString());    }        /**     * Answer a <code>StringBuffer</code> containing a message reflecting     * the current status of the message being processed.     *      * @param detailMsg     * @return StringBuffer     */    protected StringBuffer getStatusReport(String detailMsg) throws MessagingException    {        StringBuffer messageBuffer = new StringBuffer(detailMsg);        if (detailMsg.length() > 0)            messageBuffer.append(' ');        messageBuffer.append("Message ID: ");        messageBuffer.append(getMessageIn().getMessageID());        messageBuffer.append(". Flags: Seen = ");        messageBuffer.append(new Boolean(isMessageSeen()));        messageBuffer.append(", Delete = ");        messageBuffer.append(new Boolean(isMessageDeleted()));        messageBuffer.append('.');        return messageBuffer;    }            /**     * Returns the userUndefined.     * @return boolean     */    protected boolean isUserUndefined()    {        return fieldUserUndefined;    }        /**     * Is the DELETED flag set?     * @throws MessagingException     */    protected boolean isMessageDeleted() throws MessagingException    {       return getMessageIn().isSet(Flags.Flag.DELETED);    }        /**     * Is the SEEN flag set?     * @throws MessagingException     */    protected boolean isMessageSeen() throws MessagingException    {       return getMessageIn().isSet(Flags.Flag.SEEN);    }            /**     * Set the DELETED flag.     * @throws MessagingException     */    protected void setMessageDeleted() throws MessagingException    {            getMessageIn().setFlag(Flags.Flag.DELETED, true);    }            /*    /**     * Set the SEEN flag.     * @throws MessagingException     */    protected void setMessageSeen() throws MessagingException    {        // If the Seen flag is not handled by the folder        // allow a handler to do whatever it deems necessary        if (!getMessageIn()            .getFolder()            .getPermanentFlags()            .contains(Flags.Flag.SEEN))            handleMarkSeenNotPermanent();        else            getMessageIn().setFlag(Flags.Flag.SEEN, true);    }        /**     * <p>Handler for when the folder does not support the SEEN flag.     * The default behaviour implemented here is to log a warning and set the     * flag anyway.</p>     *      * <p> Subclasses may choose to override this and implement their own     * solutions.</p>     *       * @throws MessagingException     */    protected void handleMarkSeenNotPermanent() throws MessagingException    {        getMessageIn().setFlag(Flags.Flag.SEEN, true);        logStatusWarn("Message marked as SEEN, but the folder does not support a permanent SEEN flag.");    }                /**     * Returns the Blacklisted.     * @return boolean     */    protected boolean isBlacklistedRecipient()    {        return fieldBlacklistedRecipient;    }    /**     * Sets the localRecipient.     * @param localRecipient The localRecipient to set     */    protected void setRemoteRecipient(boolean localRecipient)    {        fieldRemoteRecipient = localRecipient;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -