【问题标题】:Gmail in windows FormsWindows 窗体中的 Gmail
【发布时间】:2011-06-20 14:26:39
【问题描述】:

谷歌是否提供任何服务来在 Windows 应用程序中获取电子邮件。如果没有,请给我一个简短的描述以开发相同的内容。

提前致谢。

【问题讨论】:

    标签: c# winforms gmail


    【解决方案1】:

    只需编写一个 POP3 或 IMAP 应用程序。这是处理通过 IMAP 访问 GMail 的question

    【讨论】:

      【解决方案2】:

      这是我开发并用于阅读我的 gmail 收件箱的课程。

      public class GmailClient : IDisposable
      {
          private const string GmailUri = "https://mail.google.com/mail/feed/atom";
          private string _userName;
          private string _password;
          private GmailList _newMailList;
      
          public GmailClient(string userName, string password)
          {
              _userName = userName;
              _password = password;
          }
          /// <summary>
          /// I'd prefer to return the generic list here instead of using the GetMailItem 
          /// method to get individual items, but javascript doesn't play nice with generics.
          /// </summary>
          public void GetUnreadMail()
          {
              try 
              {
                  // Get the XML feed from mail.google.com
                  XmlElement element = GetFeedXml();
      
                  if (element != null)
                  {
                      // Deserialize the transformed XML into a generic list of GmailItem objects
                      XmlNodeReader reader = new XmlNodeReader(element);
                      XmlSerializer serializer = new XmlSerializer(typeof(GmailList));
      
                      _newMailList = serializer.Deserialize(reader) as GmailList;
                  }
              }
              catch { }
          }
          /// <summary>
          /// The number of items in the unread mail collection
          /// </summary>
          public object UnreadMailCount
          {
              get 
              {
                  if (_newMailList != null)
                  {
                      return _newMailList.Count;
                  }
                  else 
                  {
                      return 0;
                  }
              }
          }
          /// <summary>
          /// Returns the GmailItem at the specified index
          /// </summary>
          /// <param name="index">Index if the mail item to return</param>
          public GmailItem GetMailItem(int index)
          {
              if (_newMailList == null || index < 0 || index > _newMailList.Count)
              {
                  throw new IndexOutOfRangeException();
              }
      
              return _newMailList[index];
          }
          /// <summary>
          /// Get the XML feed from google and transform it into a deserializable format
          /// </summary>
          private XmlElement GetFeedXml()
          {
              try
              {
                  // Create a web request to get the xml feed
                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GmailUri);
                  request.Method = "GET";
                  request.Credentials = new NetworkCredential(_userName, _password);
      
                  XmlDocument xml = null;
                  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      
                  // If the request/response is successful
                  if (response.StatusCode == HttpStatusCode.OK)
                  {
                      // Get the response stream containing the xml
                      using (XmlTextReader reader = new XmlTextReader(response.GetResponseStream()))
                      {
                          // Load the XSLT document (it's an embedded resource)
                          byte[] data = Encoding.ASCII.GetBytes(GmailReader.Properties.Resources.GmailTransform);
      
                          using (MemoryStream xsltStream = new MemoryStream(data))
                          {
                              // Create a text reader with the XSLT document
                              XmlTextReader stylesheetReader = new XmlTextReader(xsltStream);
      
                              XslCompiledTransform transform = new XslCompiledTransform();
                              transform.Load(stylesheetReader);
      
                              // Run an XSLT transform on the google feed to get an xml structure 
                              // that can be deserialized into a GmailList object
                              using (MemoryStream ms = new MemoryStream())
                              {
                                  transform.Transform(new XPathDocument(reader), null, ms);
                                  ms.Seek(0, SeekOrigin.Begin);
      
                                  xml = new XmlDocument();
                                  // Load the transformed xml
                                  xml.Load(ms);
                              }
                          }
                      }
                  }
      
                  response.Close();
      
                  return xml.DocumentElement;
              }
              catch
              {
              }
      
              return null;
          }
      
          #region IDisposable Members
      
          public void Dispose()
          {
              // Nothing to do here.
          }
      
          #endregion
      }}
      

      对不起,我忘了再添加 1 个班级,现在在这里....

      using System;
      using System.Collections.Generic;
      using System.Runtime.InteropServices;
      
      namespace GmailReader
      {
          [Serializable,
          ComVisible(true)]
          public class GmailList : List<GmailItem>
          {
              public GmailList() { }
          }
      [Serializable,
      ComVisible(true)]
      public class GmailItem
      {
          public GmailItem() { }
      
          public string Title;
          public string Summary;
          public string Link;
          public string AuthorName;
          public string AuthorEmail;
          /*public DateTime Issued;
          public DateTime Modified;*/
          public string ID;
      }
      

      }

      【讨论】:

      • 这些项目是什么:GmailList、GmailItem、GmailReader?
      • 我收到错误,像这些东西在当前上下文中不存在
      • >// 加载 XSLT 文档(它是一个嵌入式资源) byte[] data = Encoding.ASCII.GetBytes(GmailReader.Properties.Resources.GmailTransform);在哪里可以找到 GmailTransform 对象?
      【解决方案3】:

      POP3 或 IMAP 协议可以连接到 Gmail 帐户。 Google 还提供只读收件箱提要 API:http://code.google.com/apis/gmail/docs/inbox_feed.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-18
        • 1970-01-01
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多