0

我使用下面的代码从我的硬盘帐户收到的所有文件:使用谷歌驱动器,API从硬盘文件附加到一个特定的Gmail地址

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地址。

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

回答

0

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

这里是发送邮件的代码片段:

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; 
    } 

    // ... 

} 

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

希望这会有所帮助。

+0

但为此,我必须使用google-drive-api将我的Google Drive帐户中的每个文件下载到我的电脑,而不是附加每个文件,我必须使用gmail-api。 对不对? –

相关问题