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

📄 dsnbounce.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        // identifies the transaction in which the message is sent.  (see RFC3461)        // so do NOT out.println("Original-Envelope-Id:"+originalMail.getMessage().getMessageID());        //required: reporting MTA        // this is always us, since we do not translate non-internet-mail        // failure reports into DSNs        nameType = "dns";        try {            String myAddress =                (String)getMailetContext().getAttribute(Constants.HELLO_NAME);            /*            String myAddress = InetAddress.getLocalHost().getCanonicalHostName();            */            out.println("Reporting-MTA: "+nameType+"; "+myAddress);        } catch(Exception e){            // we should always know our address, so we shouldn't get here            log("WARNING: sending DSN without required Reporting-MTA Address");        }        //only for gateways to non-internet mail systems: dsn-gateway        //optional: received from        out.println("Received-From-MTA: "+nameType+"; "+originalMail.getRemoteHost());        //optional: Arrival-Date        //////////////////////////        // per recipient fields //        //////////////////////////        Iterator recipients = originalMail.getRecipients().iterator();        while (recipients.hasNext())            {                MailAddress rec = (MailAddress)recipients.next();                String addressType = "rfc822";                //required: blank line                out.println();                //optional: original recipient (see RFC3461)                //out.println("Original-Recipient: "+addressType+"; "+ ??? );                //required: final recipient                out.println("Final-Recipient: "+addressType+"; "+rec.toString());                //required: action                // alowed values: failed, delayed, delivered, relayed, expanded                // TODO: until now, we do error-bounces only                out.println("Action: failed");                //required: status                // get Exception for getting status information                // TODO: it would be nice if the SMTP-handler would set a status attribute we can use here                MessagingException ex =                    (MessagingException) originalMail.getAttribute("delivery-error");                out.println("Status: "+getStatus(ex));                //optional: remote MTA                //to which MTA were we talking while the Error occured?                //optional: diagnostic-code                String diagnosticType = null;                // this typically is the return value received during smtp                // (or other transport) communication                // and should be stored as attribute by the smtp handler                // but until now we only have error-messages.                String diagnosticCode = getErrorMsg(ex);                // Sometimes this is the smtp diagnostic code,                // but James often gives us other messages                Perl5Matcher diagMatcher = new Perl5Matcher();                boolean smtpDiagCodeAvailable =                    diagMatcher.matches(diagnosticCode, diagPattern);                if (smtpDiagCodeAvailable){                    diagnosticType = "smtp";                } else {                    diagnosticType = "X-James";                }                out.println("Diagnostic-Code: "+diagnosticType+"; "+diagnosticCode);                            //optional: last attempt                out.println("Last-Attempt-Date: "+                            rfc822DateFormat.format(((MailImpl)originalMail).getLastUpdated()));                //optional: retry until                //only for 'delayed' reports .. but we don't report this (at least until now)                //optional: extension fields            }        // setting content        dsnMessage.setText(sout.toString());        dsnMessage.saveChanges();                //dsn.setContent(sout.toString(), "text/plain");        dsn.setContent(dsnMessage, "message/delivery-status");        dsn.setDescription("Delivery Status Notification");        dsn.setFileName("status.dat");        return dsn;    }    /**     * Create a MimeBodyPart with the original Mail as Attachment     *     * @param originalMail     * @return MimeBodyPart     * @throws MessagingException     */    protected MimeBodyPart createAttachedOriginal(Mail originalMail)        throws MessagingException {        MimeBodyPart part = new MimeBodyPart();        MimeMessage originalMessage = originalMail.getMessage();        part.setContent(originalMessage, "message/rfc822");        if ((originalMessage.getSubject() != null) &&             (originalMessage.getSubject().trim().length() > 0)) {            part.setFileName(originalMessage.getSubject().trim());        } else {            part.setFileName("No Subject");        }        part.setDisposition("Attachment");        return part;    }    /**     * Guessing status code by the exception provided.     * This method should use the status attribute when the     * SMTP-handler somewhen provides it     *     * @param MessagingException     * @return status code     */    protected String getStatus(MessagingException me) {        if (me.getNextException() == null) {            String mess = me.getMessage();            Perl5Matcher m = new Perl5Matcher();            StringBuffer sb = new StringBuffer();            if (m.matches(mess, statusPattern)) {                MatchResult res = m.getMatch();                sb.append(res.group(1));                return sb.toString();            }            // bad destination system adress            if (mess.startsWith("There are no DNS entries for the hostname"))                return DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.ADDRESS_SYSTEM);            // no answer from host (4.4.1) or            // system not accepting network messages (4.3.2), lets guess ...            if (mess.equals("No mail server(s) available at this time."))                return DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.NETWORK_NO_ANSWER);            // other/unknown error            return DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.UNDEFINED_STATUS);        } else {            String retVal = null;            Exception ex1 = me.getNextException();            Perl5Matcher m = new Perl5Matcher ();            StringBuffer sb = new StringBuffer();            if (m.matches(ex1.getMessage(), statusPattern)) {                MatchResult res = m.getMatch();                sb.append(res.group(1));                return sb.toString();            } else if (ex1 instanceof SendFailedException) {                // other/undefined protocol status                // if we get an smtp returncode starting with 4                // it is an persistent transient error, else permanent                if (ex1.getMessage().startsWith("4")) {                    return DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.DELIVERY_OTHER);                } else return DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_OTHER);            } else if (ex1 instanceof UnknownHostException) {                // bad destination system address                return DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.ADDRESS_SYSTEM);            } else if (ex1 instanceof ConnectException) {                // bad connection                return DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.NETWORK_CONNECTION);            } else if (ex1 instanceof SocketException) {                // bad connection                return DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.NETWORK_CONNECTION);            } else {                // other/undefined/unknown error                return DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.UNDEFINED_STATUS);            }        }    }    /**     * Utility method for getting the error message from the (nested) exception.     * @param MessagingException     * @return error message     */    protected String getErrorMsg(MessagingException me) {        if (me.getNextException() == null) {            return me.getMessage().trim();        } else {            Exception ex1 = me.getNextException();            return ex1.getMessage().trim();        }    }    /**     * 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();    }    /**     * 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     */    protected String newName(MailImpl 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();    }    public String getMailetInfo() {        return "DSNBounce Mailet";    }    /* ******************************************************************** */    /* ****************** Begin of getX and setX methods ****************** */    /* ******************************************************************** */    /** Gets the expected init parameters.  */    protected  String[] getAllowedInitParameters() {        String[] allowedArray = {            "debug",            "passThrough",            "messageString",            "attachment",            "sender",            "prefix"        };        return allowedArray;    }    /**     * @return the <CODE>attachment</CODE> init parameter, or <CODE>MESSAGE</CODE> if missing     */    protected int getAttachmentType() throws MessagingException {        if(getInitParameter("attachment") == null) {            return MESSAGE;        } else {            return getTypeCode(getInitParameter("attachment"));        }    }    /**     * @return <CODE>SpecialAddress.REVERSE_PATH</CODE>     */    protected Collection getRecipients() {        Collection newRecipients = new HashSet();        newRecipients.add(SpecialAddress.REVERSE_PATH);        return newRecipients;    }    /**     * @return <CODE>SpecialAddress.REVERSE_PATH</CODE>     */    protected InternetAddress[] getTo() {        InternetAddress[] apparentlyTo = new InternetAddress[1];        apparentlyTo[0] = SpecialAddress.REVERSE_PATH.toInternetAddress();        return apparentlyTo;    }    /**     * @return <CODE>SpecialAddress.NULL</CODE> (the meaning of bounce)     */    protected MailAddress getReversePath(Mail originalMail) {        return SpecialAddress.NULL;    }    /* ******************************************************************** */    /* ******************* End of getX and setX methods ******************* */    /* ******************************************************************** */}

⌨️ 快捷键说明

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