android - 如何启动 android 应用程序与 GMail 帐户交谈?

标签 android service gmail gmail-imap

我正在开发一个已经存在的应用程序,但我开发它只是为了学习。我正在为 Android 开发 GMail 客户端应用程序。我尝试使用 Content Observer,但无法找到 GMail 收件箱的 Content Provider URI。我正在通过设置 ua 客户端来尝试其他方式。我的客户端已设置,但我不知道如何实例化此客户端以访问 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", "rj@gmail.com");
        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



    }




}

将所有信息收集到属性对象后,我不知道该怎么办。请提出一些建议。

问候, 拉胡尔贾斯瓦尔

最佳答案

添加此代码以连接邮件服务器

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());
        }
      }

关于android - 如何启动 android 应用程序与 GMail 帐户交谈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6424167/

相关文章:

java - 我应该在 Post 方法中设置哪个 IP 地址?

android - 在 ViewPager 中设置页面预览

android - 当应用或服务崩溃时,是否有一些API(或方法)用于获取日志?

c# - 如何从 c# 网络服务发布到 facebook 墙?

html - Gmail 标记为带有 h​​tml 和 anchor 链接的垃圾邮件

android - Kotlin 支持字段有什么用?

java - spring框架中根据实体对象调用不同的Service api

emacs - 如何在 Gmail 中使用 GNUS?

Java发送电子邮件: smtp. gmail.com错误

java - Python 代码转换为 JVM