Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- 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.Store;
- import javax.mail.internet.MimeMultipart;
- public class CarregaEmails {
- private List<EmailBean> emails;
- public CarregaEmails() {
- emails = new ArrayList<EmailBean>();
- }
- /**
- * Método responsável por fazer um backup dentro do e-mail
- */
- private void backup(Store store, Folder folderInbox, String backupFolder,
- Message... messages) throws MessagingException {
- Folder folderBackup = store.getFolder(backupFolder);
- folderBackup.open(Folder.READ_WRITE);
- folderInbox.copyMessages(messages, folderBackup);
- folderBackup.close(true);
- }
- /**
- * Método responsável por salvar o xml dentro do computador
- */
- private boolean saveFile(Part part) {
- try {
- InputStream is = part.getInputStream();
- File temp = new File("temp");
- if (!temp.exists()) {
- if (!temp.mkdir()) {
- return false;
- }
- }
- FileOutputStream fos = new FileOutputStream(temp.getAbsolutePath() + "/" + part.getFileName());
- int x = -1;
- while ((x = is.read()) != -1) {
- fos.write((char) x);
- }
- fos.flush();
- fos.close();
- return true;
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- } catch (MessagingException e) {
- e.printStackTrace();
- return false;
- } catch (Throwable e) {
- e.printStackTrace();
- return false;
- }
- }
- /*
- * Metodo responsavel por pegar o corpo do e-mail
- */
- public String getBody(Message message) throws IOException, MessagingException {
- // verifica se a mensagem veio em partes
- if(message.getContent() instanceof Multipart){
- Multipart mp = (Multipart) message.getContent();
- // percorre as partes da mensagem
- for(int i = 0; i < mp.getCount(); i++){
- Part part = mp.getBodyPart(i);
- // verifica se alguma das partes veio em outras partes, aconte quando vem e-mails com anexo
- if(part.getContent() instanceof MimeMultipart){
- MimeMultipart mm = (MimeMultipart) part.getContent();
- return mm.getBodyPart(0).getContent().toString();
- } else {
- return part.getContent().toString();
- }
- }
- }
- return "";
- }
- /*
- * Metodo responsavel por pegar o anexo do e-mail
- */
- public Part getFileAttachment(Message message) throws MessagingException, IOException{
- if(message.getContent() instanceof Multipart){
- Multipart mp = (Multipart) message.getContent();
- for(int i = 0; i < mp.getCount(); i++){
- Part part = mp.getBodyPart(i);
- if(part.getDisposition() != null && part.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)){
- if(part.getFileName().endsWith("xml")){
- return part;
- }
- }
- }
- }
- return null;
- }
- public List<EmailBean> getFiles() {
- // itera sobre todas as contas
- for (Conta conta : ContasIO.getContas()) {
- try {
- Store store = conta.getStore();
- store.connect();
- Folder folder = store.getFolder(conta.getInbox());
- folder.open(Folder.READ_WRITE);
- // itera por todas as mensagens
- for (Message message : folder.getMessages()) {
- EmailBean email = new EmailBean();
- email.setSubject(message.getSubject());
- email.setFrom(message.getFrom()[0].toString());
- email.setDate(message.getSentDate());
- email.setContent(getBody(message));
- Part part = getFileAttachment(message);
- if (part != null) {
- email.setFileName(part.getFileName());
- if (saveFile(part)) {
- emails.add(email);
- // verifica se o protocolo é imap e se existe uma pasta para backup
- if (conta.getProtocolo().contains("imap") &&
- conta.getBackup() != null && !conta.getBackup().equals("")) {
- backup(store, folder, conta.getBackup(), message);
- }
- }
- message.setFlag(Flags.Flag.DELETED, true);
- }
- }
- folder.close(true);
- store.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }
- return emails;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment