如何在Android中使用Javamail保存下载的电子邮件

时间:2016-09-29 14:04:33

标签: java android javamail

我现在构建一个电子邮件应用程序android,所以我想将电子邮件保存到设备,有人告诉eml文件并序列化你自己的类!任何人都可以指导我真正的方向请帮助我!如何使用onScrollListner()方法快速加载所有邮箱,因为现在它很慢

我使用asynctask来加载它!这是我的代码:

private class CheckMailInboxTask extends AsyncTask<Integer, Void, Message[]>{
    private int count;
    private ProgressDialog progressDialog;
    private Message[] messages;
    private boolean statusMail = true;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Loading mail");
        progressDialog.setMessage("Please wait...");
        progressDialog.show();
    }
    @Override
    protected Message[] doInBackground(Integer... params) {
        try {
            messages = AccueilActivity.emailManager.getMails();
            EmailManager.inbox.addMessageCountListener(new MessageCountAdapter() {
                @Override
                public void messagesAdded(MessageCountEvent e) {
                    super.messagesAdded(e);
                    System.out.println("New Message");
                    try {
                        Message[] messages = e.getMessages();
                        int count = messages.length;
                        for(int i=(count - 1);i >= 0;i--){
                            final MimeMessage msg = (MimeMessage) messages[i];
                            Mail email = null;
                            try {
                                email = new Mail(msg);
                                email.set_box(true);
                                Mail.INBOX.add(0,email);
                                Mail.LISTBOOL.add(Boolean.valueOf(true));//Nampiako
                                if(BoiteReceptionActivity.handler != null){
                                    BoiteReceptionActivity.handler.sendEmptyMessage(1);
                                    ab.setSubtitle("1 mail non lu(s)");
                                }
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }
                    } catch (MessagingException emx) {
                    }
                }
            });
            new Thread(new Runnable() {

                @Override
                public void run() {
                    System.out.println("UUUUUUUUUUUUUUUUUUUUU");
                    if (EmailManager.inbox instanceof IMAPFolder) {
                        final IMAPFolder imapFolder = (IMAPFolder)EmailManager.inbox;
                        startListening(imapFolder);
                    }
                    System.out.println("DDDDDDDDDDDDDDDDDDDDD");
                }
            }).start();

            count = messages.length;
            for(int i=(count - 1);i >= 0;i--){
                MimeMessage msg = (MimeMessage) messages[i];
                Mail email = null;
                try {
                    if(msg.isSet(Flags.Flag.SEEN)){
                        email = new Mail(msg);
                        email.set_box(true);
                        Mail.INBOX.add(email);
                        Mail.LISTBOOL.add(Boolean.valueOf(false));
                        Log.d("SIZE OF MAIL_INBOX SEEN", ""+Mail.INBOX.size());
                        Log.d("SIZE OF BOOL SEEN", ""+Mail.LISTBOOL.get(0));

                    }else {
                        email = new Mail(msg);
                        email.set_box(true);
                        Mail.INBOX.add(email);
                        Mail.LISTBOOL.add(Boolean.valueOf(true));
                        Log.d("SIZE OF MAIL_INBOX RECENT", ""+Mail.INBOX.size());
                        Log.d("SIZE OF BOOL RECENT", ""+Mail.LISTBOOL.size());

                    }
                    AccueilActivity.emailManager.setInboxList(Mail.INBOX);
                    new Thread(new Runnable() {      
                        @Override
                        public void run() {
                            BoiteReceptionActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //Mail.COUNT_MESSAGES = Mail.INBOX.size();
                                    Mail.in_adapteur.notifyDataSetChanged();
                                    progressDialog.dismiss();

                                }
                            });
                        }
                    }).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        } catch (MessagingException e) {
        }
        return messages;
    }


    @Override
    protected void onPostExecute(Message[] messages) {
        super.onPostExecute(messages);

    }
}

修改 这是我的客户适配器

public class AdapterBoiteReception extends BaseAdapter{
   private Context context;
   private List<Mail> listMail;
   private List<Boolean> listBool;

   public AdapterBoiteReception(Context ctx, List<Mail> list, List<Boolean> listBool){
      this.context = ctx;
      this.listMail = list;
      this.listBool = listBool;
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
     return listMail.size();
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
     return listMail.get(position);
 }

 @Override
 public long getItemId(int position) {
     // TODO Auto-generated method stub
     return listMail.indexOf(getItem(position));
 }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     View view =convertView;
        Holder mailHolder = null;
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(view == null){
            view = mInflater.inflate(R.layout.boite_reception_items, null);
            mailHolder = new Holder();

            mailHolder.nomTV = (TextView) view.findViewById(R.id.nomMsgTV);
            mailHolder.objetTV = (TextView) view.findViewById(R.id.objectMsgTV);
            mailHolder.dateTV = (TextView) view.findViewById(R.id.dateMsgTV);
            mailHolder.img = (ImageButton) view.findViewById(R.id.imgBtnAttache);

            view.setTag(mailHolder);
        }else
            mailHolder = (Holder) view.getTag();
        Mail mail = listMail.get(position);
        Log.d("VALUE OF LISTBOOL", ""+Boolean.valueOf(listBool.get(position)));
        if(Boolean.valueOf(listBool.get(position)).equals(Boolean.TRUE)){
            mailHolder.nomTV.setTypeface(null, Typeface.BOLD);
            mailHolder.objetTV.setTypeface(null, Typeface.BOLD);
            mailHolder.dateTV.setTypeface(null, Typeface.BOLD);

            mailHolder.nomTV.setText(mail.getNom());
            mailHolder.objetTV.setText(mail.getSubject());
            try {
                mailHolder.dateTV.setText(mail.getDate());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            if(mail.getHandlers().size() > 0)
                mailHolder.img.setVisibility(View.VISIBLE);
            else
                mailHolder.img.setVisibility(View.INVISIBLE);

        }else{
            view.setBackgroundColor(Color.GRAY);
            mailHolder.nomTV.setText(mail.getNom());
            mailHolder.objetTV.setText(mail.getSubject());
            try {
                mailHolder.dateTV.setText(mail.getDate());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            if(mail.getHandlers().size() > 0)
                mailHolder.img.setVisibility(View.VISIBLE);
            else
                mailHolder.img.setVisibility(View.INVISIBLE);
        }

    return view;

}
static class Holder{
    TextView nomTV;
    TextView objetTV;
    TextView dateTV;
    ImageButton img;
}

}

修改

@the_dani也有同样的问题link

1 个答案:

答案 0 :(得分:0)

使用Message.writeTo方法将邮件副本保存到FileOutputStream。 (Message是一个Part,Part定义了writeTo方法。)