📄 simplemessageattributes.java
字号:
} } response.add(SP); if (replyTo != null && replyTo.length >0) { if (replyTo[0].indexOf("@") == -1) { response.add(LB + (String)response.get(3) + RB); //first From address } else { response.add(LB); for (int i=0; i<replyTo.length; i++) { response.add( parseAddress(replyTo[i])); } response.add(RB); } } else { if (from != null && from.length > 0) { response.add(LB + (String)response.get(3) + RB); //first From address } else { response.add( NIL); } } response.add(SP); if (to != null && to.length >0) { response.add(LB); for (int i=0; i<to.length; i++) { response.add( parseAddress(to[i])); } response.add(RB); } else { response.add( NIL); } response.add(SP); if (cc != null && cc.length >0) { response.add(LB); for (int i=0; i<cc.length; i++) { response.add( parseAddress(cc[i])); } response.add(RB); } else { response.add( NIL); } response.add(SP); if (bcc != null && bcc.length >0) { response.add(LB); for (int i=0; i<bcc.length; i++) { response.add( parseAddress(bcc[i])); } response.add(RB); } else { response.add( NIL); } response.add(SP); if (inReplyTo != null && inReplyTo.length>0) { response.add( inReplyTo[0]); } else { response.add( NIL); } response.add(SP); if (messageID != null && messageID.length>0) { response.add(Q + messageID[0] + Q); } else { response.add( NIL); } response.add(RB); StringBuffer buf = new StringBuffer(16 * response.size()); for (int j=0; j<response.size(); j++) { buf.append((String)response.get(j)); } return buf.toString(); } /** * Parses a String email address to an IMAP address string. */ String parseAddress(String address) { int comma = address.indexOf(","); StringBuffer buf = new StringBuffer(); if (comma == -1) { //single address buf.append(LB); InternetAddress netAddr = null; try { netAddr = new InternetAddress(address); } catch (AddressException ae) { return null; } String personal = netAddr.getPersonal(); if (personal != null && (!personal.equals(""))) { buf.append(Q + personal + Q); } else { buf.append( NIL); } buf.append( SP); buf.append( NIL) ; // should add route-addr buf.append( SP); try { MailAddress mailAddr = new MailAddress(netAddr); buf.append(Q + mailAddr.getUser() + Q); buf.append(SP); buf.append(Q + mailAddr.getHost() + Q); } catch (ParseException pe) { buf.append( NIL + SP + NIL); } buf.append(RB); } else { buf.append(parseAddress(address.substring(0, comma))); buf.append(SP); buf.append(parseAddress(address.substring(comma + 1))); } return buf.toString(); } /** * Decode a content Type header line into types and parameters pairs */ void decodeContentType(String rawLine) { int slash = rawLine.indexOf("/"); if( slash == -1){ if (DEBUG) getLogger().debug("decoding ... no slash found"); return; } else { primaryType = rawLine.substring(0, slash).trim(); } int semicolon = rawLine.indexOf(";"); if (semicolon == -1) { if (DEBUG) getLogger().debug("decoding ... no semicolon found"); secondaryType = rawLine.substring(slash + 1).trim(); return; } // have parameters parameters = new HashSet(); secondaryType = rawLine.substring(slash + 1, semicolon).trim(); int pos = semicolon; int nextsemi = rawLine.indexOf(";", pos+1); while (nextsemi != -1) { if (DEBUG) getLogger().debug("decoding ... found another semicolon"); String param = rawLine.substring(pos + 1, nextsemi); int esign = param.indexOf("=") ; if (esign == -1) { if (DEBUG) getLogger().debug("Whacky parameter found: " + param); } else { String name = param.substring(0, esign).trim(); String value = param.substring(esign + 1).trim(); parameters.add(name + SP + value); if (DEBUG) getLogger().debug("Found parameter: " + name + SP + value); } pos = nextsemi; nextsemi = rawLine.indexOf(";", pos +1); } String lastParam = rawLine.substring(pos + 1); int esign = lastParam.indexOf("=") ; if (esign == -1) { if (DEBUG) getLogger().debug("Whacky parameter found: " + lastParam); } else { String name = lastParam.substring(0, esign).trim(); String value = lastParam.substring(esign + 1).trim(); parameters.add(Q + name + Q + SP + Q + value + Q); if (DEBUG) getLogger().debug("Found parameter: " + name + SP + value); } } String parseBodyFields() { StringBuffer buf = new StringBuffer(); if (parameters == null || parameters.isEmpty()) { buf.append(NIL); } else { buf.append(LB); Iterator it = parameters.iterator(); while(it.hasNext()) { buf.append((String)it.next()); } buf.append(RB); } buf.append(SP); if(contentID == null) { buf.append(NIL); } else { buf.append(Q + contentID + Q); } buf.append(SP); if(contentDesc == null) { buf.append(NIL); } else { buf.append(Q + contentDesc + Q); } buf.append(SP); if(contentEncoding == null) { buf.append( NIL ); } else { buf.append(Q + contentEncoding + Q); } buf.append(SP); buf.append(size); return buf.toString(); } /** * Produce the IMAP formatted String for the BodyStructure of a pre-parsed MimeMessage * TODO handle extension elements - Content-disposition, Content-Language and other parameters. */ String parseBodyStructure() { try { String fields = parseBodyFields(); StringBuffer buf = new StringBuffer(); buf.append(LB); if (primaryType.equalsIgnoreCase("Text")) { buf.append("\"TEXT\" \"" ); buf.append( secondaryType.toUpperCase() ); buf.append( "\" "); buf.append( fields ); buf.append( " " ); buf.append( lineCount ); // is: * 1 FETCH (BODYSTRUCTURE ("Text" "plain" NIL NIL NIL NIL 4 -1)) // wants: * 1 FETCH (BODYSTRUCTURE ("text" "plain" NIL NIL NIL "8bit" 6 1 NIL NIL NIL)) // or: * 1 FETCH (BODYSTRUCTURE ("text" "plain" NIL NIL NIL "7bit" 28 1 NIL NIL NIL)) } else if (primaryType.equalsIgnoreCase(MESSAGE) && secondaryType.equalsIgnoreCase("rfc822")) { buf.append("\"MESSAGE\" \"RFC822\" "); buf.append(fields + SP); setupLogger(parts[0]); // reset transient logger buf.append(parts[0].getEnvelope() + SP); buf.append(parts[0].getBodyStructure( false ) + SP); buf.append(lineCount); } else if (primaryType.equalsIgnoreCase(MULTIPART)) { for (int i=0; i<parts.length; i++) { setupLogger(parts[i]); // reset transient getLogger() buf.append(parts[i].getBodyStructure( false )); } buf.append(SP + secondaryType); } buf.append(RB); return buf.toString(); } catch (Exception e) { getLogger().error("Exception while parsing BodyStrucuture: " + e); e.printStackTrace(); throw new RuntimeException("Exception in parseBodyStructure"); } } /** * Provides the current Message Sequence Number for this message. MSNs * change when messages are expunged from the mailbox. * * @return int a positive non-zero integer */ public int getMessageSequenceNumber() { return messageSequenceNumber; } void setMessageSequenceNumber(int newMsn) { messageSequenceNumber = newMsn; } /** * Provides the unique identity value for this message. UIDs combined with * a UIDValidity value form a unique reference for a message in a given * mailbox. UIDs persist across sessions unless the UIDValidity value is * incremented. UIDs are not copied if a message is copied to another * mailbox. * * @return int a 32-bit value */ public int getUID() { return uid; } /** * Provides the date and time at which the message was received. In the * case of delivery by SMTP, this SHOULD be the date and time of final * delivery as defined for SMTP. In the case of messages copied from * another mailbox, it shuld be the internalDate of the source message. In * the case of messages Appended to the mailbox, example drafts, the * internalDate is either specified in the Append command or is the * current dat and time at the time of the Append. * * @return Date imap internal date */ public Date getInternalDate() { return internalDate; } public String getInternalDateAsString() { return internalDateString; } /** * Provides the sizeof the message in octets. * * @return int number of octets in message. */ public int getSize() { return size; } /** * Provides the Envelope structure information for this message. This is a parsed representation of the rfc-822 envelope information. This is not to be confused with the SMTP envelope! * * @return String satisfying envelope syntax in rfc 2060. */ public String getEnvelope() { return parseEnvelope(); } /** * Provides the Body Structure information for this message. This is a parsed representtion of the MIME structure of the message. * * @return String satisfying body syntax in rfc 2060. */ public String getBodyStructure( boolean includeExtensions ) { return parseBodyStructure(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -