📄 mailutil.java
字号:
for (int i = 0; i < count; i++) level = accessPart(mp.getBodyPart(i), level, attachments); level--; } else if (p.isMimeType("message/rfc822")) { level++; level = accessPart((Part) p.getContent(), level, attachments); level--; } else { byte[] content; Object o = p.getContent(); if (o instanceof String) { content = ((String)o).getBytes(Constants.UTF8_ENCODING); } else if (o instanceof InputStream) { InputStream is = (InputStream) o; ByteArray ba = new ByteArray(p.getSize() > 0 ? p.getSize() : 1024, is); content = ba.toByteArray(); } else { content = (o.toString()).getBytes(Constants.UTF8_ENCODING); } if (p instanceof MimeBodyPart && Constants.EMAIL_TRANSFER_ENCODING.equals(((MimeBodyPart)p).getEncoding())) { content = Base64.decode((String)p.getContent()); } AttachmentHolder a = new AttachmentHolder(fileName, ct, content); attachments.add(a); } }catch (Exception e) { throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, MailUtil.class.getName(), "Can't access email attachments", e); } return level; } public static int dumpPart(Part p, int level) throws Exception { if (level > 0 && p instanceof Message) dumpEnvelope((Message) p, level); String ct = p.getContentType(); try { pr("CONTENT-TYPE: " + (new ContentType(ct)).toString(), level); } catch (ParseException pex) { pr("BAD CONTENT-TYPE: " + ct, level); } String filename = p.getFileName(); if (filename != null) pr("FILENAME: " + filename, level); /* * Using isMimeType to determine the content type avoids fetching the * actual content data until we need it. */ if (p.isMimeType("text/plain")) { pr("", level); pr("=========This is plain text, level="+level+"===========", level); if (!showStructure && !saveAttachments) System.out.println((String) p.getContent()); } else if (p.isMimeType("multipart/*")) { pr("=========This is a Multipart " + level + "==================", level); Multipart mp = (Multipart) p.getContent(); level++; int count = mp.getCount(); for (int i = 0; i < count; i++) level = dumpPart(mp.getBodyPart(i), level); level--; } else if (p.isMimeType("message/rfc822")) { pr("This is a Nested Message", level); pr("===========================", level); level++; level = dumpPart((Part) p.getContent(), level); level--; } else { if (!showStructure && !saveAttachments) { /* * If we actually want to see the data, and it's not a MIME type we * know, fetch it and check its Java type. */ Object o = p.getContent(); if (o instanceof String) { pr("===============This is a string, level="+level+"============", level); System.out.println((String) o); } else if (o instanceof InputStream) { pr("========This is a binary input stream, level="+level+"======", level); InputStream is = (InputStream) o; int c; System.out.print('['); while ((c = is.read()) != -1) { if (c == 0) System.out.print('*'); else System.out.write(c); } System.out.print(']'); } else { pr("================This is an unknown type, level="+level+"===========", level); pr(o.toString(), level); } } else { // just a separator pr("********************************", level); } } /* * If we're saving attachments, write out anything that looks like an * attachment into an appropriately named file. Don't overwrite existing * files to prevent mistakes. */ if (saveAttachments && level != 0 && !p.isMimeType("multipart/*")) { String disp = p.getDisposition(); // many mailers don't include a Content-Disposition if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) { if (filename == null) filename = "Attachment" + attnum++; pr("Saving attachment to file " + filename, level); try { File f = new File(filename); if (f.exists()) // XXX - could try a series of names throw new IOException("file exists"); OutputStream os = new BufferedOutputStream(new FileOutputStream( f)); InputStream is = p.getInputStream(); int c; while ((c = is.read()) != -1) os.write(c); os.close(); } catch (IOException ex) { pr("Failed to save attachment: " + ex, level); } pr("---------------------------", level); } } return level; } public static void dumpEnvelope(Message m, int level) throws Exception { pr("------ This is the message envelope START ------", level); Address[] a; // FROM if ((a = m.getFrom()) != null) { for (int j = 0; j < a.length; j++) pr("FROM: " + a[j].toString(), level); } // TO if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { for (int j = 0; j < a.length; j++) { pr("TO: " + a[j].toString(), level); InternetAddress ia = (InternetAddress) a[j]; if (ia.isGroup()) { InternetAddress[] aa = ia.getGroup(false); for (int k = 0; k < aa.length; k++) pr(" GROUP: " + aa[k].toString(), level); } } } // SUBJECT pr("SUBJECT: " + m.getSubject(), level); // DATE Date d = m.getSentDate(); pr("SendDate: " + (d != null ? d.toString() : "UNKNOWN"), level); // FLAGS (not supported by POP3) Flags flags = m.getFlags(); StringBuffer sb = new StringBuffer(); Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags boolean first = true; for (int i = 0; i < sf.length; i++) { String s; Flags.Flag f = sf[i]; if (f == Flags.Flag.ANSWERED) s = "\\Answered"; else if (f == Flags.Flag.DELETED) s = "\\Deleted"; else if (f == Flags.Flag.DRAFT) s = "\\Draft"; else if (f == Flags.Flag.FLAGGED) s = "\\Flagged"; else if (f == Flags.Flag.RECENT) s = "\\Recent"; else if (f == Flags.Flag.SEEN) s = "\\Seen"; else continue; // skip it if (first) first = false; else sb.append(' '); sb.append(s); } String[] uf = flags.getUserFlags(); // get the user flag strings for (int i = 0; i < uf.length; i++) { if (first) first = false; else sb.append(' '); sb.append(uf[i]); } pr("FLAGS: " + sb.toString(), level); pr("HEADERS", level); Enumeration e = m.getAllHeaders(); while (e.hasMoreElements()) { javax.mail.Header head = (javax.mail.Header) e.nextElement(); pr(head.getName() + ": " + head.getValue(), level); } pr("------ This is the message envelope END ------", level); } static String indentStr = " "; /** * Print a, possibly indented, string. */ public static void pr(String s, int level) { if (showStructure) System.out.print(indentStr.substring(0, level * 2)); System.out.println(s); }}/* Without attachment: * ------ This is the message envelope START ------FROM: Marcel Ruff <marcel@localhost>TO: marcel@localhostSUBJECT: Subject, without attachmentSendDate: Thu Oct 27 17:00:10 CEST 2005FLAGS:HEADERSReturn-Path: <marcel@localhost>Received: from localhost ([127.0.0.1]) by noty (JAMES SMTP Server 2.2.0) with SMTP ID 204 for <marcel@localhost>; Thu, 27 Oct 2005 17:00:11 +0200 (CEST)Message-ID: <4360EB7A.5060401@localhost>Date: Thu, 27 Oct 2005 17:00:10 +0200From: Marcel Ruff <marcel@localhost>User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050716)X-Accept-Language: en-us, enMIME-Version: 1.0To: marcel@localhostSubject: Subject, without attachmentContent-Type: text/plain; charset=ISO-8859-1; format=flowedContent-Transfer-Encoding: 7bitDelivered-To: marcel@localhost------ This is the message envelope END ------CONTENT-TYPE: text/plain; format=flowed; charset=ISO-8859-1=========This is plain text, level=0===========Some body text! *//* * Example with a zip attachment:=========MESSAGE #1:=================------ This is the message envelope START ------FROM: Marcel Ruff <marcel@localhost>TO: marcel@localhostSUBJECT: Subject with binary attachmentSendDate: Thu Oct 27 16:51:00 CEST 2005FLAGS:HEADERSReturn-Path: <marcel@localhost>Received: from localhost ([127.0.0.1]) by noty (JAMES SMTP Server 2.2.0) with SMTP ID 137 for <marcel@localhost>; Thu, 27 Oct 2005 16:51:00 +0200 (CEST)Message-ID: <4360E954.4060409@localhost>Date: Thu, 27 Oct 2005 16:51:00 +0200From: Marcel Ruff <marcel@localhost>User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050716)X-Accept-Language: en-us, enMIME-Version: 1.0To: marcel@localhostSubject: Subject with binary attachmentContent-Type: multipart/mixed; boundary="------------050806000406010507060909"Delivered-To: marcel@localhost------ This is the message envelope END ------CONTENT-TYPE: multipart/mixed; boundary=------------050806000406010507060909=========This is a Multipart 0==================CONTENT-TYPE: text/plain; format=flowed; charset=ISO-8859-1=========This is plain text, level=1===========This is the normal body text!CONTENT-TYPE: application/zip; name=something.zipFILENAME: something.zip========This is a binary input stream, level=1======PKj[3� sometextUT �C�CUxfHello WorldPK*//* Example with one text attachment *------ This is the message envelope START ------FROM: Marcel Ruff <marcel@localhost>TO: marcel@localhostSUBJECT: Subject with text attachmentSendDate: Thu Oct 27 16:59:36 CEST 2005FLAGS:HEADERSReturn-Path: <marcel@localhost>Received: from localhost ([127.0.0.1]) by noty (JAMES SMTP Server 2.2.0) with SMTP ID 687 for <marcel@localhost>; Thu, 27 Oct 2005 16:59:36 +0200 (CEST)Message-ID: <4360EB58.7060205@localhost>Date: Thu, 27 Oct 2005 16:59:36 +0200From: Marcel Ruff <marcel@localhost>User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050716)X-Accept-Language: en-us, enMIME-Version: 1.0To: marcel@localhostSubject: Subject with text attachmentContent-Type: multipart/mixed; boundary="------------060303010309020708030505"Delivered-To: marcel@localhost------ This is the message envelope END ------CONTENT-TYPE: multipart/mixed; boundary=------------060303010309020708030505=========This is a Multipart 0==================CONTENT-TYPE: text/plain; format=flowed; charset=ISO-8859-1=========This is plain text, level=1===========Some body text!CONTENT-TYPE: application/unknown; name=sometext.txtFILENAME: sometext.txt========This is a binary input stream, level=1======Hello World*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -