我正在编写一个java程序来遍历Exchange服务器上的所有电子邮件并将它们移动到一个文件夹中。我正在通过电子邮件正确迭代,并从Multipart消息中找到了附件,但是当我需要保存文件时。我收到错误:" BAD命令参数错误。 11&#34 ;.我搜索过去几天并没有找到导致这种情况的原因。是什么导致了这个问题?
这是我的代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
import javax.mail.search.FlagTerm;
import com.sun.mail.util.BASE64DecoderStream;
public class EmailAttachmentMoverIMAP {
private static Store store = null;
public static void main(String args[]) {
store = buildStore();
Message[] messages = getMessages();
iterateAttachments(messages);
try{store.close();}catch (MessagingException e){e.printStackTrace();}
}
public static Store buildStore(){
Properties properties = System.getProperties();
properties = new Properties();
properties.setProperty("mail.host", "MYHOST");
properties.setProperty("mail.port", "143");
properties.setProperty("mail.transport.protocol", "imap");
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("USER", "PASS");
}
});
try {
store = session.getStore("imap");
}
catch (Exception e){
e.printStackTrace();
}
return store;
}
public static Message[] getMessages(){
Message[] messages = null;
try
{
store.connect();
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
messages = inbox.search(unseenFlagTerm);
if (messages.length == 0) System.out.println("No messages found.");
}
catch (MessagingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return messages;
}
public static void iterateAttachments(Message[] messages){
System.out.println("Moving "+messages.length + " attachments");
for (int i=0; i<messages.length; i++){
System.out.println("moving attachment "+i+" of "+messages.length);
moveAttachments(messages[i], i);
}
}
public static void moveAttachments(Message message, int num){
try{
Address[] fromAddress = message.getFrom();
String from = fromAddress[0].toString();
String subject = message.getSubject();
String sentDate = message.getSentDate().toString();
String contentType = message.getContentType();
String messageContent = "";
// store attachment file name, separated by comma
String attachFiles = "";
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile("C:\\skyspark-2.1.8\\db\\demo\\io\\" + File.separator + fileName);
} else {
// this part may be the excel file
if(part.getContentType().equalsIgnoreCase("application/vnd.ms-excel")){
getPartDetails(part);
part.saveFile("C:\\skyspark-2.1.8\\db\\demo\\io\\" + File.separator + part.getFileName());
}
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain")
|| contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}
System.out.println("\t From: " + from);
System.out.println("\t Subject: " + subject);
System.out.println("\t Sent Date: " + sentDate);
System.out.println("\t Message: " + messageContent);
System.out.println("\t Attachments: " + attachFiles);
}
catch(Exception e){
e.printStackTrace();
}
}
public static void getPartDetails(MimeBodyPart part){
try{
System.out.println("part content class: "+part.getContent().getClass().toString());
System.out.println("part content Type: "+part.getContentType());
System.out.println("part description: "+part.getDescription());
System.out.println("part disposition: "+part.getDisposition());
System.out.println("part encoding: "+part.getEncoding());
System.out.println("part file name: "+part.getFileName());
System.out.println("part line count: "+part.getLineCount());
System.out.println("part size: "+part.getSize());
Enumeration headers = part.getAllHeaders();
int i = 0;
while (headers.hasMoreElements()){
Header hdr = (Header) headers.nextElement();
System.out.println("header "+i+" name: "+hdr.getName());
System.out.println("header "+i+" value: "+hdr.getValue());
i++;
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
以下是输出示例:
Moving 3975 attachments
moving attachment 0 of 3975
part content class: class com.sun.mail.util.BASE64DecoderStream
part content Type: application/vnd.ms-excel
part description: null
part disposition: inline
part encoding: base64
part file name: Temp 2015 Mar 23 0601.csv
part line count: -1
part size: 5342
header 0 name: Content-Type
header 0 value: application/vnd.ms-excel
header 1 name: Content-Transfer-Encoding
header 1 value: base64
header 2 name: Content-Disposition
header 2 value: inline; filename="Temp 2015 Mar 23 0601.csv"
java.io.IOException: A8 BAD Command Argument Error. 11
at com.sun.mail.imap.IMAPInputStream.fill(IMAPInputStream.java:154)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:213)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:239)
at com.sun.mail.util.BASE64DecoderStream.getByte(BASE64DecoderStream.java:358)
at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:249)
at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:144)
at java.io.FilterInputStream.read(Unknown Source)
at javax.mail.internet.MimeBodyPart.saveFile(MimeBodyPart.java:825)
at javax.mail.internet.MimeBodyPart.saveFile(MimeBodyPart.java:851)
at EmailAttachmentMoverIMAP.moveAttachments(EmailAttachmentMoverIMAP.java:111)
moving attachment 1 of 3975
at EmailAttachmentMoverIMAP.iterateAttachments(EmailAttachmentMoverIMAP.java:79)
at EmailAttachmentMoverIMAP.main(EmailAttachmentMoverIMAP.java:29)
part content class: class com.sun.mail.util.BASE64DecoderStream
part content Type: application/vnd.ms-excel
part description: null
part disposition: inline
part encoding: base64
part file name: Temperature 2015 Mar 23 0601.csv
part line count: -1
part size: 5342
header 0 name: Content-Type
header 0 value: application/vnd.ms-excel
header 1 name: Content-Transfer-Encoding
header 1 value: base64
header 2 name: Content-Disposition
header 2 value: inline; filename="Temperature 2015 Mar 23 0601.csv"
java.io.IOException: A13 BAD Command Argument Error. 11
at com.sun.mail.imap.IMAPInputStream.fill(IMAPInputStream.java:154)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:213)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:239)
at com.sun.mail.util.BASE64DecoderStream.getByte(BASE64DecoderStream.java:358)
at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:249)
at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:144)
at java.io.FilterInputStream.read(Unknown Source)
at javax.mail.internet.MimeBodyPart.saveFile(MimeBodyPart.java:825)
at javax.mail.internet.MimeBodyPart.saveFile(MimeBodyPart.java:851)
at EmailAttachmentMoverIMAP.moveAttachments(EmailAttachmentMoverIMAP.java:111)
at EmailAttachmentMoverIMAP.iterateAttachments(EmailAttachmentMoverIMAP.java:79)
at EmailAttachmentMoverIMAP.main(EmailAttachmentMoverIMAP.java:29)moving attachment 2 of 3975
正如您所看到的,我发现了附件(它有文件名和文件大小),但我无法写入输出流(我试图获取输入流并在每个输出流中写入可能的方式并收到相同的问题)。
编辑:启用协议调试:
DEBUG: JavaMail version 1.4.7
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: protocolConnect returning false, host=ACTUALHOSTADDRESS, user=ACTUALUSER, password=<null>
DEBUG IMAP: trying to connect to host "ACTUALHOSTADDRESS", port 143, isSSL false
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN STARTTLS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=ACTUALHOSTADDRESS, user=ACTUALUSERNAME, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 OK AUTHENTICATE completed.
A2 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN STARTTLS CHILDREN IDLE NAMESPACE LITERAL+
A2 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: connection available -- size: 1
A3 SELECT INBOX
* 6184 EXISTS
* 254 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
* OK [UNSEEN 5323] Is the first unseen message
* OK [UIDVALIDITY 141703] UIDVALIDITY value
* OK [UIDNEXT 6298] The next unique identifier value
A3 OK [READ-WRITE] SELECT completed.
A4 SEARCH UNSEEN ALL
* SEARCH 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184
A4 OK SEARCH completed.
Moving 860 attachments
moving attachment 0 of 860
A5 FETCH 5323 (ENVELOPE INTERNALDATE RFC822.SIZE)
* 5323 FETCH (UID 5430 ENVELOPE ("Thu, 30 Apr 2015 10:44:59 -0500" "=?utf-8?B?TWljcm9zb2Z0IE91dGxvb2sgVGVzdCBNZXNzYWdl?=" (("Microsoft Outlook" NIL "USER" "HOST")) NIL NIL (("=?utf-8?B?TktDc2QgU3Bhcms=?=" NIL "nkcsdspark" "HOST")) NIL NIL NIL "<2ac2507b-1f41-4a49-8835-cf396ffefced@HOST.com>") INTERNALDATE "30-Apr-2015 10:44:59 -0500" RFC822.SIZE 839)
A5 OK FETCH completed.
A6 FETCH 5323 (BODYSTRUCTURE)
* 5323 FETCH (UID 5430 BODYSTRUCTURE ("text" "html" ("charset" "utf-8") NIL NIL "8bit" 181 2 NIL NIL NIL NIL))
A6 OK FETCH completed.
A7 FETCH 5323 (BODY[TEXT]<0.181>)
A7 BAD Command Argument Error. 11
DEBUG IMAP: IMAPProtocol noop
A8 NOOP
A8 OK NOOP completed.
moving attachment 1 of 860
A9 FETCH 5324 (ENVELOPE INTERNALDATE RFC822.SIZE)
java.io.IOException: A7 BAD Command Argument Error. 11
at com.sun.mail.imap.IMAPInputStream.fill(IMAPInputStream.java:154)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:213)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at com.sun.mail.handlers.text_plain.getContent(text_plain.java:125)
at javax.activation.DataSourceDataContentHandler.getContent(Unknown Source)
at javax.activation.DataHandler.getContent(Unknown Source)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1420)
at EmailAttachmentMoverCSCIMAP.moveAttachments(EmailAttachmentMoverCSCIMAP.java:118)
at EmailAttachmentMoverCSCIMAP.iterateAttachments(EmailAttachmentMoverCSCIMAP.java:75)
at EmailAttachmentMoverCSCIMAP.main(EmailAttachmentMoverCSCIMAP.java:23)