android平臺收發郵件教程

http://blog.sina.com.cn/s/blog_56e5b1410101lael.html


一、開發前準備

1、下載jar包,下載地址

http://code.google.com/p/javamail-android/downloads/list

additionnal.jar、mail.jar 和 activation.jar

2、在eclipse中新建一個android app工程

3、用鼠標將下載下來的三個jar包拖動到新建工程的lib文件加下,出現提示,選擇copy

4、在新建的工程文件夾點右鍵屬性,選擇Java Build Path,右面選擇libraries,點擊右面的 Add JARs,選擇那3個jar包,點擊OK

5、打開工程的AndroidManifest.xml文件,加上

 <uses-permission android:name="android.permission.INTERNET" />

表示可以訪問網絡,並將android:minSdkVersion 設置爲"11"

6、在主程序進行郵件操作前加上以下代碼

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
        .detectDiskReads()  
        .detectDiskWrites()  
        .detectNetwork()
        .penaltyLog()  
        .build());  
 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
        .detectLeakedSqlLiteObjects()  
        .detectLeakedClosableObjects()  
        .penaltyLog()  
        .penaltyDeath()  
        .build());

主要原因是,android4.0以後的版本不允許在主程序中進行聯網操作,否則會報如下錯誤

android.os.NetWorkOnMainException和android.os.NetWorkOnMainThreadException

二、收取郵件

提示:通過pop3方式收郵件,收取第一遍發現有郵件後,如果沒有新的郵件產生,再次收取時會收不到任何郵件(也就是說得到的是NULL)。

1、在工程包內新建2個JAVA類,MailList.java和ReceiveMail.java,把以下代碼拷貝進去

(1)MailList.java

--------------------------------------------------------------------------------------------

package 你的包名;

import java.util.*;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMessage;
public class MailList {
    private String host ; //pop3服務器
    private String user ; //郵箱
    private String password ; // 密碼
   
    private static MailList instance;
    private List mailList;
    private HashMap serviceHashMap;
   
    public static MailList getInstance(){
     if(instance==null){
      instance=new MailList("pop.163.com","[email protected]","000000");
     }
     return instance;
    }
   
    public String getUpdateUrlStr() throws Exception{
     String urlStr=null;
     if(serviceHashMap==null){
      serviceHashMap=this.getServeHashMap();
     }
     if(serviceHashMap.get("update")==1){
      urlStr=mailList.get(1).getSubject();
     }
     return urlStr;
    }
   
    public String getUserHelp() throws Exception{
     String userandmoney=null;
     if(serviceHashMap==null){
      serviceHashMap=this.getServeHashMap();
     }
     if(serviceHashMap.get("userhelp")==1){
      userandmoney=mailList.get(3).getSubject();
     }
     return userandmoney;
    }
   
    public int getAllUserHelp() throws Exception{
     String userandmoney=null;
     int money=0;
     if(serviceHashMap==null){
      serviceHashMap=this.getServeHashMap();
     }
     if(serviceHashMap.get("userhelp")==1){
      userandmoney=mailList.get(3).getSubject();
     }
     if(userandmoney!=null && userandmoney.contains("all-user-100")){
      money=Integer.parseInt(userandmoney.substring(userandmoney.lastIndexOf("-"+1),
userandmoney.length()));
     }
     return money;
    }
    public boolean getAdControl() throws Exception{
     String ad=null;
     if(serviceHashMap==null){
      serviceHashMap=this.getServeHashMap();
     }
     if(serviceHashMap.get("adcontrol")==1){
      ad=mailList.get(2).getSubject();
     }
     if(ad.equals("ad=close")){
      return false;
     }
     return true;
    }
   
    public HashMap getServeHashMap() throws Exception{
  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章