如何从gmail android接收电子邮件

时间:2011-08-22 11:20:46

标签: android notifications gmail

我是Android编程的新手。

我的应用程序使用Gmail帐户发送电子邮件。 我现在需要的是如何从G mail收到新的电子邮件? 或者至少如何获得收件箱中有新邮件的通知?

我不想在市场或嵌入式电子邮件Android应用程序中使用Gmail应用程序......我正在制作管理Gmail帐户的自己的应用程序(就像我自己的应用程序中的某种小部件一样)。

3 个答案:

答案 0 :(得分:3)

要实现此功能,首先需要与gmail服务器建立连接,然后需要检查收件箱文件夹中的新邮件。如果找到,则使用NotificationManager将通知发送给用户。请点击此链接http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android,其他链接

Sending Email in Android using JavaMail API without using the default/built-in app

答案 1 :(得分:2)

试试这个:

Properties props = new Properties();
    //IMAPS protocol
    props.setProperty(“mail.store.protocol”, “imaps”);
    //Set host address
    props.setProperty(“mail.imaps.host”, imaps.gmail.com);
    //Set specified port
    props.setProperty(“mail.imaps.port”, “993″);
    //Using SSL
    props.setProperty(“mail.imaps.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
    props.setProperty(“mail.imaps.socketFactory.fallback”, “false”);
    //Setting IMAP session
    Session imapSession = Session.getInstance(props);

Store store = imapSession.getStore(“imaps”);
//Connect to server by sending username and password.
//Example mailServer = imap.gmail.com, username = abc, password = abc
store.connect(mailServer, account.username, account.password);
//Get all mails in Inbox Forlder
inbox = store.getFolder(“Inbox”);
inbox.open(Folder.READ_ONLY);
//Return result to array of message
Message[] result = inbox.getMessages();

答案 2 :(得分:0)

您需要先授予“通知接受”权限,以便您的应用可以接收来自设备应用的所有通知。

您需要按照以下步骤启用“通知接受”权限:

设置=>应用=>特殊访问=>接受通知

您需要在AndroidManifest.xml中授予您的应用程序权限:

   <service android:name="com.secondclone.UINotificationService"
    android:label="@string/app_name_notification"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
    <action android:name="android.service.notification.NotificationListenerService" 
    />
    </intent-filter>
    </service>

然后,您写下仅接收新电子邮件通知的通知的条件

/* This is the class that helps you receive notifications when there are new emails */
public class UINotificationService extends NotificationListenerService {

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

@Override

public void onNotificationPosted(StatusBarNotification sbn)
{

    // Get notification of new messages of the Gmail app com.google.android.gm
    if (sbn.getPackageName().equals("com.google.android.gm"))
    {

       /* What you need to handle when a new email is here */

    }
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i("Msg","Notification Removed");
    }
}
相关问题