Olá a todos.
Eu estou desenvolvendo uma aplicação que verifica as mensagens em uma caixa de email e salvar os anexo existentes.
Tudo funciona bem até que a aplicação tenta obter da mensagem um arquivo contendo um anexo com caracteres especias como:
Documento de EspecificaÇÂO.doc
Eu percebi que na mesagem o arquivo chega com o seguinte nome:
“=?iso-8859-1?Q?Documento_de_Especifica=E7=E3o_de_Requisitos.doc?=”
package br.com.cesan.helpdesk;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
public class LerEmail2 {
/**
* @param args
* @throws MessagingException
* @throws IOException
*/
public static void main(String[] args) throws MessagingException, IOException {
// TODO Auto-generated method stub
// Get session
Session session = Session.getInstance(new Properties(), null);
// Get the store
Store store = session.getStore("pop3");
store.connect("pop.xxxx.com.br", "usuario", "senha");
Folder folder = store.getFolder("INBOX");
//folder.open(Folder.READ_ONLY);
folder.open(Folder.READ_WRITE);
// Show only unreaded Messages
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
// Get directory
Message message[] = folder.getMessages();
//Message messages[] = folder.search(ft);
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": [" + message[i].getFrom()[0] +"]"
+ " [ " + message[i].getSubject() +"]"
+ " [ " + message[i].getSubject() +"]"
+ " [" + message[i].getSentDate() +"]"
);
Object content = message[i].getContent();
if (content instanceof Multipart) {
handleMultipart((Multipart)content);
} else {
handlePart(message[i]);
}
}
// Close connection
folder.close(false);
store.close();
}
public static void handleMultipart(Multipart multipart) throws MessagingException, IOException {
for (int i=0, n=multipart.getCount(); i><n; i++) {
handlePart(multipart.getBodyPart(i));
}
}
public static void handlePart(Part part) throws MessagingException, IOException {
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (disposition == null) { // When just body
System.out.println("Null: " + contentType);
// Check if plain
if ((contentType.length() >= 10) && (contentType.toLowerCase().substring(0, 10).equals("text/plain"))) {
part.writeTo(System.out);
} else { // Don't think this will happen
System.out.println("Other body: " + contentType);
part.writeTo(System.out);
}
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment: " + part.getFileName() + " : " + contentType);
saveFile(part.getFileName(), part.getInputStream());
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: " +
part.getFileName() +
" : " + contentType);
saveFile(part.getFileName(), part.getInputStream());
} else { // Should never happen
System.out.println("Other: " + disposition);
}
}
public static void saveFile(String filename, InputStream input) throws IOException {
if (filename == null) {
filename = File.createTempFile("xx", ".out").getName();
}
// Do no overwrite existing file
File file = new File(filename);
for (int i=0; file.exists(); i++) {
file = new File(filename+i);
}
System.setProperty("file.encoding", "iso-8859-1");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read()) != -1) {
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
}
}
O problema acontece na linha:
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read()) != -1) {
bos.write(aByte);
}
Alguêm já passou por isto?
Obs: Eu estou usando a API javamail1.4.5
Obrigado