获取Gmail中的最后一封邮件

时间:2016-02-13 05:54:33

标签: c#

我使用下面的代码来阅读来自gmail的电子邮件。但问题是它给出了未读的电子邮件。在我的情况下,我想阅读最近的04封电子邮件,无论它是否已阅读或未阅读

class ReadEmail
    {
        public void AccessEmail(string userName, string password)
        {
            try
            {
                System.Net.WebClient objClient = new System.Net.WebClient();
                string response;
                string title;
                string summary;

                //Creating a new xml document
                XmlDocument doc = new XmlDocument();
                XmlNode nr = default(XmlNode);

                //Logging in Gmail server to get data
                objClient.Credentials = new System.Net.NetworkCredential(userName, password);
                //reading data and converting to string
                response = Encoding.UTF8.GetString(
                           objClient.DownloadData(@"https://mail.google.com/mail/feed/atom"));

                response = response.Replace(
                     @"<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", @"<feed>");

                //loading into an XML so we can get information easily
                doc.LoadXml(response);


                nr = doc.SelectSingleNode(@"/feed/fullcount");

                //Reading the title and the summary for every email
                foreach (XmlNode node in doc.SelectNodes(@"/feed/entry"))
                {
                    title = node.SelectSingleNode("title").InnerText;
                    summary = node.SelectSingleNode("summary").InnerText;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
            }


        }

使用上述代码我无法看到更完整的电子邮件正文。

1 个答案:

答案 0 :(得分:1)

gmail原子Feed只会为您提供电子邮件摘要。如果你需要完整的身体,你必须使用其他协议,如POP3或iMap。

这是一个答案,告诉你如何做到这一点: Count number of emails in gmail using IMAP