C# 在Gmail中获取最后的邮件

C# 在Gmail中获取最后的邮件,c#,C#,我用下面的代码来阅读gmail的邮件。但问题是它会发送未读的电子邮件。在我的情况下,我想阅读过去04封电子邮件,无论它是否已阅读 class ReadEmail { public void AccessEmail(string userName, string password) { try { System.Net.WebClient objClient = new Syst

我用下面的代码来阅读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);
            }


        }
class-ReadEmail
{
public void AccessEmail(字符串用户名、字符串密码)
{
尝试
{
System.Net.WebClient objClient=新系统.Net.WebClient();
字符串响应;
字符串标题;
字符串摘要;
//创建新的xml文档
XmlDocument doc=新的XmlDocument();
XmlNode nr=默认值(XmlNode);
//登录Gmail服务器获取数据
objClient.Credentials=新系统.Net.NetworkCredential(用户名、密码);
//读取数据并转换为字符串
响应=Encoding.UTF8.GetString(
objClient.DownloadData(@)https://mail.google.com/mail/feed/atom"));
响应=响应。替换(
@"", @"");
//加载到XML中,以便轻松获取信息
doc.LoadXml(响应);
nr=单据选择单节点(@“/feed/fullcount”);
//阅读每封电子邮件的标题和摘要
foreach(doc.SelectNodes(@“/feed/entry”)中的XmlNode节点)
{
title=节点。选择SingleNode(“title”)。InnerText;
summary=节点。选择SingleNode(“summary”)。InnerText;
}
}
捕获(例外情况除外)
{
系统。诊断。调试。写入(例如消息);
}
}

用上面的代码我看不到更多完整的电子邮件正文。

gmail atom提要只会给你一个电子邮件摘要。如果你需要完整的正文,你必须使用其他协议,如POP3或iMap

下面的答案向您展示了如何做到这一点: