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

📄 abstractredirect.java

📁 java mail,java mailjava mailjava mailjava mail
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                            .append(", message=").append(messageText)                            .append(", recipients=").append(arrayToString(recipients == null ? null : recipients.toArray()))                            .append(", subject=").append(subject)                            .append(", subjectPrefix=").append(subjectPrefix)                            .append(", apparentlyTo=").append(arrayToString(apparentlyTo))                            .append(", attachError=").append(attachError)                            .append(", isReply=").append(isReply)                            .append(", attachmentType=").append(attachmentType)                            .append(", inLineType=").append(inLineType)                            .append(" ");                log(logBuffer.toString());            }        }    }    /**     * Service does the hard work,and redirects the originalMail in the form specified.     *     * @param originalMail the mail to process and redirect     * @throws MessagingException if a problem arises formulating the redirected mail     */    public void service(Mail originalMail) throws MessagingException {        boolean keepMessageId = false;        // duplicates the Mail object, to be able to modify the new mail keeping the original untouched        MailImpl newMail = new MailImpl(originalMail,newName(originalMail));        try {            // We don't need to use the original Remote Address and Host,            // and doing so would likely cause a loop with spam detecting            // matchers.            try {                newMail.setRemoteAddr(java.net.InetAddress.getLocalHost().getHostAddress());                newMail.setRemoteHost(java.net.InetAddress.getLocalHost().getHostName());            } catch (java.net.UnknownHostException _) {                newMail.setRemoteAddr("127.0.0.1");                newMail.setRemoteHost("localhost");            }                if (isDebug) {                log("New mail - sender: " + newMail.getSender()                           + ", recipients: " + arrayToString(newMail.getRecipients().toArray())                           + ", name: " + newMail.getName()                           + ", remoteHost: " + newMail.getRemoteHost()                           + ", remoteAddr: " + newMail.getRemoteAddr()                           + ", state: " + newMail.getState()                           + ", lastUpdated: " + newMail.getLastUpdated()                           + ", errorMessage: " + newMail.getErrorMessage());            }                //Create the message            if(getInLineType(originalMail) != UNALTERED) {                if (isDebug) {                    log("Alter message");                }                newMail.setMessage(new MimeMessage(Session.getDefaultInstance(System.getProperties(),                                                                   null)));                    // handle the new message if altered                buildAlteredMessage(newMail, originalMail);                } else {                // if we need the original, create a copy of this message to redirect                if (getPassThrough(originalMail)) {                    newMail.setMessage(new MimeMessage(originalMail.getMessage()) {                        protected void updateHeaders() throws MessagingException {                            if (getMessageID() == null) super.updateHeaders();                            else {                                modified = false;                            }                        }                    });                }                if (isDebug) {                    log("Message resent unaltered.");                }                keepMessageId = true;            }                //Set additional headers                setRecipients(newMail, getRecipients(originalMail), originalMail);                setTo(newMail, getTo(originalMail), originalMail);                setSubjectPrefix(newMail, getSubjectPrefix(originalMail), originalMail);                if(newMail.getMessage().getHeader(RFC2822Headers.DATE) == null) {                newMail.getMessage().setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date()));            }                setReplyTo(newMail, getReplyTo(originalMail), originalMail);                setReversePath(newMail, getReversePath(originalMail), originalMail);                setSender(newMail, getSender(originalMail), originalMail);                setIsReply(newMail, isReply(originalMail), originalMail);                newMail.getMessage().saveChanges();                if (keepMessageId) {                setMessageId(newMail, originalMail);            }                if (senderDomainIsValid(newMail)) {                //Send it off...                getMailetContext().sendMail(newMail);            } else {                StringBuffer logBuffer = new StringBuffer(256)                                        .append(getMailetName())                                        .append(" mailet cannot forward ")                                        .append(originalMail.getName())                                        .append(". Invalid sender domain for ")                                        .append(newMail.getSender())                                        .append(". Consider using the Resend mailet ")                                        .append("using a different sender.");                throw new MessagingException(logBuffer.toString());            }            } finally {            newMail.dispose();        }                if(!getPassThrough(originalMail)) {            originalMail.setState(Mail.GHOST);        }    }    private static final java.util.Random random = new java.util.Random();  // Used to generate new mail names    /**     * Create a unique new primary key name.     *     * @param mail the mail to use as the basis for the new mail name     * @return a new name     */    private String newName(Mail mail) throws MessagingException {        String oldName = mail.getName();                // Checking if the original mail name is too long, perhaps because of a        // loop caused by a configuration error.        // it could cause a "null pointer exception" in AvalonMailRepository much        // harder to understand.        if (oldName.length() > 76) {            int count = 0;            int index = 0;            while ((index = oldName.indexOf('!', index + 1)) >= 0) {                count++;            }            // It looks like a configuration loop. It's better to stop.            if (count > 7) {                throw new MessagingException("Unable to create a new message name: too long."                                             + " Possible loop in config.xml.");            }            else {                oldName = oldName.substring(0, 76);            }        }                StringBuffer nameBuffer =                                 new StringBuffer(64)                                 .append(oldName)                                 .append("-!")                                 .append(random.nextInt(1048576));        return nameBuffer.toString();    }    /**     * A private method to convert types from string to int.     *     * @param param the string type     * @return the corresponding int enumeration     */    protected int getTypeCode(String param) {        param = param.toLowerCase(Locale.US);        if(param.compareTo("unaltered") == 0) {            return UNALTERED;        }        if(param.compareTo("heads") == 0) {            return HEADS;        }        if(param.compareTo("body") == 0) {            return BODY;        }        if(param.compareTo("all") == 0) {            return ALL;        }        if(param.compareTo("none") == 0) {            return NONE;        }        if(param.compareTo("message") == 0) {            return MESSAGE;        }        return NONE;    }    /**     * Utility method for obtaining a string representation of an array of Objects.     */    private String arrayToString(Object[] array) {        if (array == null) {            return "null";        }        StringBuffer sb = new StringBuffer(1024);        sb.append("[");        for (int i = 0; i < array.length; i++) {            if (i > 0) {                sb.append(",");            }            sb.append(array[i]);        }        sb.append("]");        return sb.toString();    }    /**     * Utility method for obtaining a string representation of a     * Message's headers     */    protected String getMessageHeaders(MimeMessage message) throws MessagingException {        Enumeration heads = message.getAllHeaderLines();        StringBuffer headBuffer = new StringBuffer(1024);        while(heads.hasMoreElements()) {            headBuffer.append(heads.nextElement().toString()).append("\r\n");        }        return headBuffer.toString();    }    /**     * Utility method for obtaining a string representation of a     * Message's body     */    private String getMessageBody(MimeMessage message) throws Exception {        java.io.ByteArrayOutputStream bodyOs = new java.io.ByteArrayOutputStream();        MimeMessageUtil.writeMessageBodyTo(message,bodyOs);        return bodyOs.toString();    }    /**     * Builds the message of the newMail in case it has to be altered.     *     * @param originalMail the original Mail object     * @param newMail the Mail object to build     */    protected void buildAlteredMessage(Mail newMail, Mail originalMail) throws MessagingException {        MimeMessage originalMessage = originalMail.getMessage();        MimeMessage newMessage = newMail.getMessage();        // Copy the relevant headers        String[] relevantHeaderNames =            {RFC2822Headers.DATE,             RFC2822Headers.FROM,             RFC2822Headers.REPLY_TO,             RFC2822Headers.TO,             RFC2822Headers.SUBJECT,             RFC2822Headers.RETURN_PATH};        Enumeration headerEnum = originalMessage.getMatchingHeaderLines(relevantHeaderNames);        while (headerEnum.hasMoreElements()) {            newMessage.addHeaderLine((String) headerEnum.nextElement());        }        StringWriter sout = new StringWriter();        PrintWriter out   = new PrintWriter(sout, true);        String head = getMessageHeaders(originalMessage);        boolean all = false;        String messageText = getMessage(originalMail);        if(messageText != null) {            out.println(messageText);        }        if (isDebug) {            log("inline:" + getInLineType(originalMail));        }        switch(getInLineType(originalMail)) {            case ALL: //ALL:                all = true;            case HEADS: //HEADS:                out.println("Message Headers:");                out.println(head);                if(!all) {                    break;                }            case BODY: //BODY:                out.println("Message:");                try {                    out.println(getMessageBody(originalMessage));                } catch(Exception e) {                    out.println("body unavailable");                }                break;            default:            case NONE: //NONE:                break;        }        try {            //Create the message body            MimeMultipart multipart = new MimeMultipart("mixed");            // Create the message            MimeMultipart mpContent = new MimeMultipart("alternative");            MimeBodyPart contentPartRoot = new MimeBodyPart();            contentPartRoot.setContent(mpContent);            multipart.addBodyPart(contentPartRoot);            MimeBodyPart part = new MimeBodyPart();            part.setText(sout.toString());            part.setDisposition("inline");            mpContent.addBodyPart(part);            if (isDebug) {                log("attachmentType:" + getAttachmentType(originalMail));            }            if(getAttachmentType(originalMail) != NONE) {

⌨️ 快捷键说明

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