温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - Get messages which contain an attachment via gmail API

c# - 通过gmail API获取包含附件的邮件

发布于 2020-03-29 13:11:13

我正在基于Gmail API构建应用程序。我可以看到当前收件箱中的所有邮件,但是我需要将其限制为仅包含附件的邮件。我怎样才能做到这一点?

这是我的GoogleController.cs:

[HttpGet]
[Authorize]
public async Task<IActionResult> GetListEmail(string LabelId, string nameLabel)
{
    string UserEmail = User.FindFirst(c => c.Type == ClaimTypes.Email).Value;
    var service = GetService();
    List<My_Message> listMessages = new List<My_Message>();
    List<Message> result = new List<Message>();
    var emailListRequest = service.Users.Messages.List(UserEmail);
    emailListRequest.LabelIds = LabelId;
    emailListRequest.IncludeSpamTrash = false;
    emailListRequest.MaxResults = 1000;
    var emailListResponse = await emailListRequest.ExecuteAsync();

    if (emailListResponse != null && emailListResponse.Messages != null)
    {
        foreach (var email in emailListResponse.Messages)
        {
            var emailInfoRequest = service.Users.Messages.Get(UserEmail, email.Id);
            var emailInfoResponse = await emailInfoRequest.ExecuteAsync();
            if (emailInfoResponse != null)
            {
                My_Message message = new My_Message();
                message.Id = listMessages.Count + 1;
                message.EmailId = email.Id;
                foreach (var mParts in emailInfoResponse.Payload.Headers)
                {
                    if (mParts.Name == "Date")
                        message.Date_Received = mParts.Value;
                    else if (mParts.Name == "From")
                        message.From = mParts.Value;
                    else if (mParts.Name == "Subject")
                        message.Title = mParts.Value;
                }
                listMessages.Add(message);
            }
        }
    }
    ViewBag.Message = nameLabel;
    return View("~/Views/Home/Index.cshtml", listMessages);
}

查看更多

查看更多

提问者
Gerbut_1986
被浏览
57
Raserhin 2020-01-31 17:24

我认为一个更简单的解决方案是在Users.messages.list端点中查询而无需创建过滤器。

实际上,您可以q像在GMail搜索框中那样使用该参数进行查询,如果不熟悉,可以查看整个运算符列表

实际上,在文档中有一个使用此查询参数的示例

using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;

using System.Collections.Generic;

// ...

public class MyClass {

  // ...

  /// <summary>
  /// List all Messages of the user's mailbox matching the query.
  /// </summary>
  /// <param name="service">Gmail API service instance.</param>
  /// <param name="userId">User's email address. The special value "me"
  /// can be used to indicate the authenticated user.</param>
  /// <param name="query">String used to filter Messages returned.</param>
  public static List<Message> ListMessages(GmailService service, String userId, String query)
  {
      List<Message> result = new List<Message>();
      UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List(userId);
      request.Q = query; // inform this with the right query

      do
      {
          try
          {
              ListMessagesResponse response = request.Execute();
              result.AddRange(response.Messages);
              request.PageToken = response.NextPageToken;
          }
          catch (Exception e)
          {
              Console.WriteLine("An error occurred: " + e.Message);
          }
      } while (!String.IsNullOrEmpty(request.PageToken));

      return result;
  }

  // ...

}

因此,对于您的情况,您只需要添加行

emailListRequest.Q = "has:attachment";

在执行请求之前,这样做不会为您的帐户创建一个完整的过滤器,因此,这种情况可能更方便。

发布
问题

分享
好友

手机
浏览

扫码手机浏览