2011-06-21 84 views
1

我正在開發一個已經存在的應用程序,但我正在開發只是爲了學習。我正在開發面向Android的GMail客戶端應用程序。我嘗試過使用Content Observer,但無法找到GMail Inbox的Content Provider URI。我想通過設置客戶端來嘗試其他方式。我的客戶端已建立,但我不知道如何實例化此客戶端以擊中GMail服務器。如何啓動Android應用程序與GMail帳戶通話?

以下是我的代碼:

GMailInbox.java

package com.tyco.gmailApp; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class GmailInbox extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //GmailObserver go = new GmailObserver(); 
     //this.getApplicationContext().getContentResolver().registerContentObserver(Uri.parse("content://imap.gmail.com"), true, go); 

     Intent myService = new Intent(this,GMailService.class); 
     myService.putExtra("user", "[email protected]"); 
     myService.putExtra("password", "brooklyn"); 
     startService(myService); 
    } 
} 

GMailService.java

package com.tyco.gmailApp; 


import java.util.Properties; 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.view.inputmethod.InputMethodSession; 
import android.view.inputmethod.InputMethod.SessionCallback; 
public class GMailService extends Service 
{ 

    private String mailhost = "imaps.gmail.com"; 
    private String user; 
    private String password; 

    @Override 
    public IBinder onBind(Intent intent){ 
     return null; 
    } 

    @Override 
    public void onCreate(){ 
     super.onCreate(); 


    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 
     this.user = intent.getStringExtra("user"); 
     this.password = intent.getStringExtra("password");; 
     Properties props = new Properties(); 
     props = System.getProperties(); 
     props.setProperty("mail.store.protocol", "imaps"); 
     props.setProperty("mail.imaps.host", mailhost); 
     props.put("mail.imaps.auth", "true"); 
     props.put("mail.imaps.port", "993"); 
     props.put("mail.imaps.socketFactory.port", "993"); 
     props.put("mail.imaps.socketFactory.class", 
        "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.imaps.socketFactory.fallback", "false"); 
     props.put("mail.imaps.socketFactory.fallback", "false"); 
     props.setProperty("mail.imaps.quitwait", "false"); 


// I Don't Know what to do here 



    } 




} 

我不知道所有收集到的信息後做什麼屬性對象。請提出建議。

問候, 拉胡爾·賈斯沃爾

回答

1

添加該代碼與郵件服務器

Session session = Session.getDefaultInstance(props, null); 
    try{ 
     Store store = session.getStore("imaps"); 
     store.connect(mailhost, mailid,password);  
     Folder folder = store.getFolder("INBOX"); 
     folder.open(Folder.READ_ONLY); 
     int count=folder.getUnreadMessageCount(); 
     Message message[] = folder.getMessages(); 
     for (int i=0, n=message.length; i<n; i++) { 
       System.out.println("subject " +message[i].getSubject()); 
     } 
     } 
+0

連接我嘗試這樣的代碼,但會議在javax.mail包中定義了一類不被支持的Android。 – Rahul

+0

我將mail-1.4_1.jar添加到我的項目中。現在我收到以下錯誤: javax.mail.MessagingException:主機未解析:imaps.gmail.com; 嵌套的異常是: java.net.SocketException:主機未解決:imaps.gmail.com 我解決了它通過修改我的mailHost url爲「imap.gmail.com」而不是「imaps.gmail.com」 – Rahul

相關問題