【问题标题】:Use Google Drive-api to attach files from Drive to a specific Gmail address使用 Google Drive-api 将文件从 Drive 附加到特定的 Gmail 地址
【发布时间】:2017-08-27 10:19:21
【问题描述】:

我使用以下代码从我的云端硬盘帐户接收所有文件:

    static void Main(string[] args)
    {
        UserCredential credential;

        using (var stream =
            new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

        // Create Drive API service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.
        FilesResource.ListRequest listRequest = service.Files.List();
        listRequest.PageSize = 10;
        listRequest.Fields = "nextPageToken, files(id, name)";

        // List files.
        IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
            .Files;
        Console.WriteLine("Files:");
        if (files != null && files.Count > 0)
        {
            foreach (var file in files)
            {
                Console.WriteLine("{0} ({1})", file.Name, file.Id);
            }
        }
        else
        {
            Console.WriteLine("No files found.");
        }
        Console.Read();

    }


}

运行后,我得到: enter image description here

太好了,我的所有文件都保存在我的云端硬盘帐户中。

现在,我想获取每个文件并附加并发送到特定的 Gmail 地址。

任何想法我现在应该做什么?

【问题讨论】:

    标签: c# google-api google-drive-android-api google-drive-realtime-api


    【解决方案1】:

    这可能仍取决于您的实施,但您可以使用 Gmail API 附加驱动器文件并将其发送给特定用户。

    这里是发送邮件的代码sn-p:

    using Google.Apis.Gmail.v1;
    using Google.Apis.Gmail.v1.Data;
    
    // ...
    
    public class MyClass {
    
      // ...
    
      /// <summary>
      /// Send an email from the user's mailbox to its recipient.
      /// </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="email">Email to be sent.</param>
      public static Message SendMessage(GmailService service, String userId, Message email)
      {
          try
          {
              return service.Users.Messages.Send(email, userId).Execute();
          }
          catch (Exception e)
          {
              Console.WriteLine("An error occurred: " + e.Message);
          }
    
          return null;
      }
    
      // ...
    
    }
    

    以下是一些可以帮助您的相关参考资料:

    希望这会有所帮助。

    【讨论】:

    • 但为此,我必须使用 google-drive-api 将我的 Google Drive 帐户中的每个文件下载到我的计算机,而不是附加每个文件,我必须使用 gmail-api。我说的对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    • 2020-03-10
    • 2020-06-18
    • 1970-01-01
    相关资源
    最近更新 更多