📄 abstractredirect.java
字号:
part = new MimeBodyPart(); switch(getAttachmentType(originalMail)) { case HEADS: //HEADS: part.setText(head); break; case BODY: //BODY: try { part.setText(getMessageBody(originalMessage)); } catch(Exception e) { part.setText("body unavailable"); } break; case ALL: //ALL: StringBuffer textBuffer = new StringBuffer(1024) .append(head) .append("\r\nMessage:\r\n") .append(getMessageBody(originalMessage)); part.setText(textBuffer.toString()); break; case MESSAGE: //MESSAGE: part.setContent(originalMessage, "message/rfc822"); break; } if ((originalMessage.getSubject() != null) && (originalMessage.getSubject().trim().length() > 0)) { part.setFileName(originalMessage.getSubject().trim()); } else { part.setFileName("No Subject"); } part.setDisposition("Attachment"); multipart.addBodyPart(part); } //if set, attach the original mail's error message if (attachError(originalMail) && originalMail.getErrorMessage() != null) { part = new MimeBodyPart(); part.setContent(originalMail.getErrorMessage(), "text/plain"); part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain"); part.setFileName("Reasons"); part.setDisposition(javax.mail.Part.ATTACHMENT); multipart.addBodyPart(part); } newMail.getMessage().setContent(multipart); newMail.getMessage().setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType()); } catch (Exception ioe) { throw new MessagingException("Unable to create multipart body", ioe); } } /** * Sets the message id of originalMail into newMail. */ private void setMessageId(Mail newMail, Mail originalMail) throws MessagingException { String messageId = originalMail.getMessage().getMessageID(); if (messageId != null) { newMail.getMessage().setHeader(RFC2822Headers.MESSAGE_ID, messageId); if (isDebug) { log("MESSAGE_ID restored to: " + messageId); } } } /** * Returns the {@link SpecialAddress} that corresponds to an init parameter value. * The init parameter value is checked against a String[] of allowed values. * The checks are case insensitive. * * @param addressString the string to check if is a special address * @param allowedSpecials a String[] with the allowed special addresses * @return a SpecialAddress if found, null if not found or addressString is null * @throws MessagingException if is a special address not in the allowedSpecials array */ protected final MailAddress getSpecialAddress(String addressString, String[] allowedSpecials) throws MessagingException { if (addressString == null) { return null; } addressString = addressString.toLowerCase(Locale.US); addressString = addressString.trim(); MailAddress specialAddress = null; if(addressString.compareTo("postmaster") == 0) { specialAddress = getMailetContext().getPostmaster(); } if(addressString.compareTo("sender") == 0) { specialAddress = SpecialAddress.SENDER; } if(addressString.compareTo("reversepath") == 0) { specialAddress = SpecialAddress.REVERSE_PATH; } if(addressString.compareTo("from") == 0) { specialAddress = SpecialAddress.FROM; } if(addressString.compareTo("replyto") == 0) { specialAddress = SpecialAddress.REPLY_TO; } if(addressString.compareTo("to") == 0) { specialAddress = SpecialAddress.TO; } if(addressString.compareTo("recipients") == 0) { specialAddress = SpecialAddress.RECIPIENTS; } if(addressString.compareTo("delete") == 0) { specialAddress = SpecialAddress.DELETE; } if(addressString.compareTo("unaltered") == 0) { specialAddress = SpecialAddress.UNALTERED; } if(addressString.compareTo("null") == 0) { specialAddress = SpecialAddress.NULL; } // if is a special address, must be in the allowedSpecials array if (specialAddress != null) { // check if is an allowed special boolean allowed = false; for (int i = 0; i < allowedSpecials.length; i++) { String allowedSpecial = allowedSpecials[i]; allowedSpecial = allowedSpecial.toLowerCase(Locale.US); allowedSpecial = allowedSpecial.trim(); if(addressString.compareTo(allowedSpecial) == 0) { allowed = true; break; } } if (!allowed) { throw new MessagingException("Special (\"magic\") address found not allowed: " + addressString + ", allowed values are \"" + arrayToString(allowedSpecials) + "\""); } } return specialAddress; } /** * <P>Checks if a sender domain of <I>mail</I> is valid.</P> * <P>If we do not do this check, and someone uses a redirection mailet in a * processor initiated by SenderInFakeDomain, then a fake * sender domain will cause an infinite loop (the forwarded * e-mail still appears to come from a fake domain).<BR> * Although this can be viewed as a configuration error, the * consequences of such a mis-configuration are severe enough * to warrant protecting against the infinite loop.</P> * <P>This check can be skipped if {@link #getFakeDomainCheck(Mail)} returns true.</P> * * @param mail the mail object to check * @return true if the if the sender is null or * {@link org.apache.mailet.MailetContext#getMailServers} returns true for * the sender host part */ protected final boolean senderDomainIsValid(Mail mail) throws MessagingException { if (getFakeDomainCheck(mail)) { return mail.getSender() == null || getMailetContext().getMailServers(mail.getSender().getHost()).size() != 0; } else return true; } /** * Checks if there are unallowed init parameters specified in the configuration file * against the String[] allowedInitParameters. */ private void checkInitParameters(String[] allowedArray) throws MessagingException { // if null then no check is requested if (allowedArray == null) { return; } Collection allowed = new HashSet(); Collection bad = new ArrayList(); for (int i = 0; i < allowedArray.length; i++) { allowed.add(allowedArray[i]); } Iterator iterator = getInitParameterNames(); while (iterator.hasNext()) { String parameter = (String) iterator.next(); if (!allowed.contains(parameter)) { bad.add(parameter); } } if (bad.size() > 0) { throw new MessagingException("Unexpected init parameters found: " + arrayToString(bad.toArray())); } } /** * It changes the subject of the supplied message to to supplied value * but it also tries to preserve the original charset information.<BR> * * This method was needed to avoid sending the subject using a charset * (usually the default charset on the server) which doesn't contain * the characters in the subject, resulting in the loss of these characters. * The most simple method would be to either send it in ASCII unencoded * or in UTF-8 if non-ASCII characters are present but unfortunately UTF-8 * is not yet a MIME standard and not all email clients * are supporting it. The optimal method would be to determine the best * charset by analyzing the actual characters. That would require much * more work (exept if an open source library already exists for this). * However there is nothing to stop somebody to add a detection algorithm * for a specific charset. <BR> * * The current algorithm works correctly if only ASCII characters are * added to an existing subject.<BR> * * If the new value is ASCII only, then it doesn't apply any encoding to * the subject header. (This is provided by MimeMessage.setSubject()).<BR> * * Possible enhancement: under java 1.4 java.nio the system can determine if the * suggested charset fits or not (if there is untranslatable * characters). If the charset doesn't fit the new value, it * can fall back to UTF-8.<BR> * * @param message the message of which subject is changed * @param newValue the new (unencoded) value of the subject. It must * not be null. * @throws MessagingException - according to the JavaMail doc most likely * this is never thrown */ public static void changeSubject(MimeMessage message, String newValue) throws MessagingException { String rawSubject = message.getHeader(RFC2822Headers.SUBJECT, null); String mimeCharset = determineMailHeaderEncodingCharset(rawSubject); if (mimeCharset == null) { // most likely ASCII // it uses the system charset or the value of the // mail.mime.charset property if set message.setSubject(newValue); return; } else { // original charset determined String javaCharset = javax.mail.internet.MimeUtility.javaCharset(mimeCharset); try { message.setSubject(newValue, javaCharset); } catch (MessagingException e) { // known, but unsupported encoding // this should be logged, the admin may setup a more i18n // capable JRE, but the log API cannot be accessed from here //if (charset != null) log(charset + // " charset unsupported by the JRE, email subject may be damaged"); message.setSubject(newValue); // recover } } } /** * It attempts to determine the charset used to encode an "unstructured" * RFC 822 header (like Subject). The encoding is specified in RFC 2047. * If it cannot determine or the the text is not encoded then it returns null. * * Here is an example raw text: * Subject: =?iso-8859-2?Q?leg=FAjabb_pr=F3ba_l=F5elemmel?= * * @param rawText the raw (not decoded) value of the header. Null means * that the header was not present (in this case it always return null). * @return the MIME charset name or null if no encoding applied */ static private String determineMailHeaderEncodingCharset(String rawText) { if (rawText == null) return null; int iEncodingPrefix = rawText.indexOf("=?"); if (iEncodingPrefix == -1) return null; int iCharsetBegin = iEncodingPrefix + 2; int iSecondQuestionMark = rawText.indexOf('?', iCharsetBegin); if (iSecondQuestionMark == -1) return null; // safety checks if (iSecondQuestionMark == iCharsetBegin) return null; // empty charset? impossible int iThirdQuestionMark = rawText.indexOf('?', iSecondQuestionMark + 1); if (iThirdQuestionMark == -1) return null; // there must be one after encoding if (-1 == rawText.indexOf("?=", iThirdQuestionMark + 1)) return null; // closing tag String mimeCharset = rawText.substring(iCharsetBegin, iSecondQuestionMark); return mimeCharset; } /** * Returns a new Collection built over <I>list</I> replacing special addresses * with real <CODE>MailAddress</CODE>-es.<BR> * Manages <CODE>SpecialAddress.SENDER</CODE>, <CODE>SpecialAddress.REVERSE_PATH</CODE>, * <CODE>SpecialAddress.FROM</CODE>, <CODE>SpecialAddress.REPLY_TO</CODE>, * <CODE>SpecialAddress.RECIPIENTS</CODE>, <CODE>SpecialAddress.TO</CODE>, * <CODE>SpecialAddress.NULL</CODE> and <CODE>SpecialAddress.UNALTERED</CODE>.<BR> * <CODE>SpecialAddress.FROM</CODE> is made equivalent to <CODE>SpecialAddress.SENDER</CODE>; * <CODE>SpecialAddress.TO</CODE> is made equivalent to <CODE>SpecialAddress.RECIPIENTS</CODE>.<BR> * <CODE>SpecialAddress.REPLY_TO</CODE> uses the ReplyTo header if available, otherwise the * From header if available, otherwise the Sender header if available, otherwise the return-path.<BR> * <CODE>SpecialAddress.NULL</CODE> and <CODE>SpecialAddress.UNALTERED</CODE> are ignored.<BR> * Any other address is not replaced. */ protected Collection replaceMailAddresses(Mail mail, Collection list) { Collection newList = new HashSet(list.size()); Iterator iterator = list.iterator(); while (iterator.hasNext()) { MailAddress mailAddress = (MailAddress) iterator.next(); if (!mailAddress.getHost().equalsIgnoreCase("address.marker")) { newList.add(mailAddress); } else if (mailAddress == SpecialAddress.SENDER || mailAddress == SpecialAddress.FROM) { MailAddress sender = mail.getSender(); if (sender != null) { newList.add(sender); } } else if (mailAddress == SpecialAddress.REPLY_TO) { int parsedAddressCount = 0; try { InternetAddress[] replyToArray = (InternetAddress[]) mail.getMessage().getReplyTo(); if (replyToArray != null) { for (int i = 0; i < replyToArray.length; i++) { try { newList.add(new MailAddress(replyToArray[i])); parsedAddressCount++; } catch (ParseException pe) { log("Unable to parse a \"REPLY_TO\" header address in the original message: " + replyToArray[i] + "; ignoring."); } } } } catch (MessagingException ae) { log
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -