📄 abstractredirect.java
字号:
try { byte[] block = new byte[1024]; int read = 0; while ((read = bis.read(block)) > -1) { bos.write(block, 0, read); } bos.flush(); return bodyOs.toString(); } finally { bis.close(); } } /** * 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) { 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -