如何从gmail android接收电子邮件

如何从gmail android接收电子邮件,android,notifications,gmail,Android,Notifications,Gmail,我是android编程新手 我用Gmail账户发送电子邮件。 我现在需要的是如何接收来自G mail的新电子邮件? 或者至少如何获得收件箱中有新邮件的通知 我不想使用来自市场的Gmail应用程序或嵌入式电子邮件android应用程序等等……我正在制作自己的应用程序来管理Gmail帐户(就像我自己应用程序中的某种小部件)。为了实现这一功能,首先需要与Gmail服务器建立连接,然后需要检查收件箱文件夹中的新邮件。如果找到,则使用NotificationManager向用户发送通知。请跟随此链接,另一

我是android编程新手

我用Gmail账户发送电子邮件。 我现在需要的是如何接收来自G mail的新电子邮件? 或者至少如何获得收件箱中有新邮件的通知


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

为了实现这一功能,首先需要与Gmail服务器建立连接,然后需要检查收件箱文件夹中的新邮件。如果找到,则使用NotificationManager向用户发送通知。请跟随此链接,另一个链接是

试试这个:

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

您需要首先授予“通知接受”权限,以便您的应用程序可以从设备应用程序接收任何通知

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

设置=>Apps=>Special access=>Notification accept

您需要在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>

我不确定我是否收到了,请确认。您希望您的应用程序接收电子邮件吗?你可以在Emulator中设置你的gmail帐户,然后在Emulator中获取邮件。你好,伙计,我一年前就在做这件事了……我很确定我能找到解决办法观看这些链接。祝你好运一点背景资料就好了。会话和存储的名称空间是什么?
/* 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 */
       Bundle extras = sbn.getNotification().extras;
                if (!contentGmail.equals(extras.getCharSequence("android.bigText").toString()))
                {
                    contentGmail = Objects.requireNonNull(extras.getCharSequence("android.bigText")).toString();

                    // This is the recipient's Gmail name information.
                    String mreceiver = extras.getString("android.subText");

                    // This is the sender's name.
                    String mSender = extras.getString("android.title");

                    // This is the Email subject.
                    String mSubject = Objects.requireNonNull(extras.getCharSequence("android.text")).toString();

                    // This is the text of this new mail.
                    String mContent = Objects.requireNonNull(extras.getCharSequence("android.bigText")).toString();

                    //Notification.EXTRA_TEXT
                    time = sbn.getPostTime() / 1000;
                    Log.i("tsMail", "Sender = " + mSender + " Receiver= " + receiver + " Content Gmail= " + mContent );

                    }
                }

    }
}

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