Android 如何从 Gmail 接收电子邮件

发布于 2024-11-30 16:02:23 字数 192 浏览 0 评论 0原文

我是安卓编程新手。

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

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

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

做个少女永远怀春 2024-12-07 16:02:23

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

使用 JavaMail API 在 Android 中发送电子邮件,无需使用默认/内置应用

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

溺孤伤于心 2024-12-07 16:02:23

试试这个:

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

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();
回眸一遍 2024-12-07 16:02:23

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

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

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

您需要在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 */
       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");
    }
}

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 */
       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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文