How to receive email from gmail android

时间秒杀一切 提交于 2019-12-30 05:15:09

问题


I am new to android programming.

I got my app with Gmail account sends emails. What I need now is how to receive new emails from G mail? Or at least how to get a notification that there is a new mail in my inbox?

I don't want to use Gmail app from market or embedded email android app or so...I'm making my own app that manages Gmail accounts (like some kind of widget in my own app).


回答1:


In order to implement this functionality ,first you need to establish the connection with the gmail server,then you need to check the inbox folder for new messages. If find then send the notification to the user using NotificationManager. please follow this links http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android and another link is

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




回答2:


Try this:

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



回答3:


You need to first grant permission to "Notification accept " so your app can receive any notifications from device apps.

You need to follow the steps below to enable the "Notification accept" permission:

Setting => Apps => Special access => Notification accept

You need to give your application permission in 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>

Then, you write down the conditions to only receive notifications for new email notifications

/* 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");
    }
}


来源:https://stackoverflow.com/questions/7146706/how-to-receive-email-from-gmail-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!