Android Open Source - AndroIUT Mail Reader






From Project

Back to project page AndroIUT.

License

The source code is released under:

GNU General Public License

If you think the Android project AndroIUT listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iutdijon.androiut2.mail.services;
import java.util.Properties;
// w  w w.j  a v a2 s  .  co  m
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;

/**
 * Classe 
 * @author Morgan Funtowicz
 *
 */
public class MailReader {
  
  public static enum Protocol { IMAP("imap"), POP("pop"), IMAPS("imaps"), POPS("pops"); 
    private final String protocol; 
    Protocol(String p){ 
      protocol = p;
    }
    @Override
    public String toString(){
      return protocol;
    }
  };
  
  private Protocol protocol= null;
  private String host = null;
  private boolean secured = false;
  
  private Properties props;
  private Session session;
  private Store store;
  private static Folder inbox;
  
  /**
   * Cr?er un MailReader en sp?cifiant s'il doit utiliser une surcouche SSL
   * @param pProtocol Le protocol de r?cup?ration ? utiliser <b>MailReader.Protocol</b>
   * @param pHost L'adresse du serveur o? r?cup?rer les mails
   */
  public MailReader(Protocol pProtocol, String pHost){
    protocol = pProtocol;
    host = pHost;
    if(protocol == Protocol.IMAPS || protocol == Protocol.POPS) secured = true;
    init();
  }
  
  public void connect(String username, String password) throws MessagingException{
    if(store!=null){
      store.connect(host, username, password);
    }
  }
  public void disconnect(){
    try {
      store.close();
    } catch (MessagingException e) {
      e.printStackTrace();
    }finally{
      store = null;
    }
  }
  /**
   * R?cup?re tout les messages du dossier
   * @param limit Nombre maximal de mail ? charger (limite l'impacte en m?moire)
   * @return Un tableau avec les messages du dossier;
   * @throws MessagingException
   */
  public Message[] getMessages(int limit) throws MessagingException{
    
    if(inbox == null){
      inbox = store.getFolder("Inbox");
      inbox.open(Folder.READ_WRITE);
    }
    
    int nbMessage = inbox.getMessageCount();
    if(limit >= inbox.getMessageCount()) limit = nbMessage-1;
    else if(nbMessage == 0) limit = 0;
    
    Message[] messages = inbox.getMessages(inbox.getMessageCount()-limit, inbox.getMessageCount());

    inbox.fetch(messages, fetchProfile());
    return messages;
  }
  /**
   * R?cup?re tout les messages non lus.
   * @return Un tableau de messages non lus.
   * @throws MessagingException
   */
  public Message[] getUnreadedMessages() throws MessagingException{
    inbox = store.getFolder("inbox");
    inbox.open(Folder.READ_WRITE);
    
    Message[] messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
    
    inbox.fetch(messages, fetchProfile());
    return messages;
  } 

  /**
   * Initialise le lecteur
   */
  private void init()  {
    initReaderProtocol();
    initSession();
    try {
      initStore();
    } catch (NoSuchProviderException e) {
      e.printStackTrace();
    }
  }
  
  private void initStore() throws NoSuchProviderException{
    store = session.getStore(protocol.toString());
  }
  
  private void initSession(){
    if(props!= null){
      session = Session.getDefaultInstance(props);
    }
  }
  private void initReaderProtocol(){
    props = System.getProperties();
    props.setProperty("mail.store.protocol", protocol.toString());    
  }
  
  private FetchProfile fetchProfile(){
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.CONTENT_INFO);
    fp.add(FetchProfile.Item.ENVELOPE);
    fp.add(FetchProfile.Item.FLAGS);
    
    
    return fp;
  }
  
  /**
   * 
   * @return Renvoie le protocol pour le lecteur
   */
  public Protocol getProtocol(){
    return protocol;
  }
  
  /**
   * 
   * @return Renvoie l'adresse du serveur courant
   */
  public String getHost(){
    return host;
  }
  
  /**
   * 
   * @return Renvoie un true si la connexion est s?curis?e, false sinon
   */
  public boolean isLayered(){
    return secured;
  }
}




Java Source Code List

.ADEResourcesDataTree.java
com.iutdijon.androiut2.ade.activities.ADEActivity.java
com.iutdijon.androiut2.ade.activities.ADEListResourcesChooser.java
com.iutdijon.androiut2.ade.activities.OnImageDownloadedCallback.java
com.iutdijon.androiut2.ade.service.ADEICalendarDownloader.java
com.iutdijon.androiut2.ftp.activities.FTPActivity.java
com.iutdijon.androiut2.ftp.adapters.FTPFileListAdapter.java
com.iutdijon.androiut2.ftp.data.FTPCommandResult.java
com.iutdijon.androiut2.ftp.data.FTPCommandWrapper.java
com.iutdijon.androiut2.ftp.data.FTPCommand.java
com.iutdijon.androiut2.ftp.services.FTPDownloadMonitor.java
com.iutdijon.androiut2.ftp.services.FTPServiceCall.java
com.iutdijon.androiut2.ftp.services.ProgressDownload.java
com.iutdijon.androiut2.global.AndroIUTApplication.java
com.iutdijon.androiut2.iut.activities.HomeActivity.java
com.iutdijon.androiut2.iut.activities.LoginActivity.java
com.iutdijon.androiut2.iut.data.Credential.java
com.iutdijon.androiut2.iut.data.UserFactory.java
com.iutdijon.androiut2.iut.data.account.StudentAccount.java
com.iutdijon.androiut2.iut.data.account.TeacherAccount.java
com.iutdijon.androiut2.iut.data.account.UserAccount.java
com.iutdijon.androiut2.iut.services.IUTLoginServiceCall.java
com.iutdijon.androiut2.mail.activities.MailActivity.java
com.iutdijon.androiut2.mail.activities.MailReaderActivity.java
com.iutdijon.androiut2.mail.adapters.MailDisplayAdapter.java
com.iutdijon.androiut2.mail.adapters.MailsListAdapter.java
com.iutdijon.androiut2.mail.services.EmailUtils.java
com.iutdijon.androiut2.mail.services.MailReader.java
com.iutdijon.androiut2.mail.services.MailService.java
com.iutdijon.androiut2.schooling.activities.SchoolingActivity.java
com.iutdijon.androiut2.schooling.activities.SchoolingTabActivity.java
com.iutdijon.androiut2.schooling.adapters.SectionedAdapter.java
com.iutdijon.androiut2.schooling.data.Absence.java
com.iutdijon.androiut2.schooling.data.Mark.java
com.iutdijon.androiut2.schooling.data.SchoolingData.java
com.iutdijon.androiut2.schooling.service.SchoolingDataServiceCall.java
com.iutdijon.androiut2.util.AndroIUTLogger.java
com.iutdijon.androiut2.util.IOUtils.java
com.iutdijon.androiut2.util.PreferencesManager.java
com.iutdijon.androiut2.util.UIUtil.java
com.iutdijon.androiut2.util.adapters.AbsencesAdapter.java
com.iutdijon.androiut2.util.adapters.BitmapAdapter.java
com.iutdijon.androiut2.util.adapters.CalendarAdapter.java
com.iutdijon.androiut2.util.adapters.IAdapter.java
com.iutdijon.androiut2.util.adapters.MarksAdapter.java
com.iutdijon.androiut2.util.adapters.UserAdapter.java
com.iutdijon.androiut2.util.adapters.XmlAdapter.java
com.iutdijon.androiut2.util.bridge.BridgeFinder.java
com.iutdijon.androiut2.util.loaders.AndroIUTHTTPConnection.java
com.iutdijon.androiut2.util.loaders.AsyncServiceCall.java
com.iutdijon.androiut2.util.loaders.BitmapDownloaderTask.java
com.iutdijon.androiut2.util.loaders.GetAsyncServiceCall.java
com.iutdijon.androiut2.util.loaders.ImageDownloader.java
com.iutdijon.androiut2.util.loaders.PostAsyncServiceCall.java
com.iutdijon.androiut2.util.observers.Observable.java
com.iutdijon.androiut2.util.observers.Observer.java
com.iutdijon.androiut2.util.security.SecureCenter.java
com.iutdijon.androiut2.util.view.ImageZoomView.java
com.iutdijon.androiut2.util.zoom.AspectQuotient.java
com.iutdijon.androiut2.util.zoom.DynamicZoomControl.java
com.iutdijon.androiut2.util.zoom.LongPressZoomListener.java
com.iutdijon.androiut2.util.zoom.PinchZoomListener.java
com.iutdijon.androiut2.util.zoom.SimpleZoomListener.java
com.iutdijon.androiut2.util.zoom.ZoomState.java
com.iutdijon.androiut2.util.zoom.util.Dynamics.java
com.iutdijon.androiut2.util.zoom.util.SpringDynamics.java