无法使用Google电子邮件迁移API V2与C#中的Nuget库迁移带附件的电子邮件

时间:2014-09-04 07:07:20

标签: google-api-dotnet-client

我可以上传没有附件的电子邮件,但我无法上传附件中的电子邮件。

是否有任何来源我可以查看有关如何操作的源代码?

或者可以为我提供任何可以完成项目的指针/代码存根吗?

我目前正在使用Visual Studio 2010的Nuget库。

以下是我的代码无效:

 private Task Run()
 {
 UserCredential cred;
 cred = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
            {
                ClientId = "ClientID",
                ClientSecret = "ClientSecret",
            }, new[] { Google.Apis.Admin.email_migration_v2.AdminService.Scope.EmailMigration },  "admin@example.com", System.Threading.CancellationToken.None,new FileDataStore("Email.Migration")).Result;

var service = new AdminService(new Google.Apis.Services.BaseClientService.Initializer
            {
                ApplicationName = "Email Migration",
                HttpClientInitializer = cred 
            });

       return UploadFileAsync(service);
 }   

private Task<IUploadProgress> UploadFileAsync(AdminService service)
    { 
MailItem mailitem = new MailItem(); 
mailitem.IsInbox = true;

String BodyContent = String.Empty;

BodyContent = BodyContent + "--part_boundary \r\n";
BodyContent = BodyContent + "Content-Type: message/rfc822 \r\n";
BodyContent = BodyContent + "\r\n";
BodyContent = BodyContent + "Date: Wed, 03 Jan 2013 02:56:03 -0800 \r\n";
BodyContent = BodyContent + "From: admin@example.org \r\n";
BodyContent = BodyContent + "To: liz@example.com \r\n";
BodyContent = BodyContent + "Subject: Hello World! \r\n";
BodyContent = BodyContent + "MIME-Version: 1.0 \r\n";
BodyContent = BodyContent + "Content-Type: multipart/mixed; boundary=frontier \r\n";
BodyContent = BodyContent + "\r\n";
BodyContent = BodyContent + "--frontier \r\n";
BodyContent = BodyContent + "Content-Type: text/plain; charset=windows-1252 \r\n";
BodyContent = BodyContent + "Content-Transfer-Encoding: 8bit \r\n";
BodyContent = BodyContent + "\r\n";
BodyContent = BodyContent + "And I think to myself... What a wonderful world! \r\n";
BodyContent = BodyContent + "\r\n";
BodyContent = BodyContent + "--frontier \r\n";
BodyContent = BodyContent + "Content-Type: text/plain; name=\"test.txt\" \r\n";
BodyContent = BodyContent + "Content-Disposition: attachment; filename=\"test.txt\" \r\n";
BodyContent = BodyContent + "Content-Transfer-Encoding: 8bit \r\n";
BodyContent = BodyContent + "\r\n";
BodyContent = BodyContent + "Hello A! \r\n";
BodyContent = BodyContent + "\r\n";
BodyContent = BodyContent + "--frontier-- \r\n";
BodyContent = BodyContent + "\r\n";
BodyContent = BodyContent + "--part_boundary--";

byte[] byteArray = Encoding.UTF8.GetBytes(BodyContent);
MemoryStream stream = new MemoryStream(byteArray);

String ContentType = "multipart/related; boundary=\"part_boundary\"";

  var insert = service.Mail.Insert(mailitem, "abc@example.com", stream, ContentType);
        insert.ProgressChanged += new Action<Google.Apis.Upload.IUploadProgress> (irm_ProgressChanged);
        insert.ChunkSize = MailResource.InsertMediaUpload.DefaultChunkSize * 2;
        var task = insert.UploadAsync();
        task.ContinueWith(t =>
        {
            // NotOnRanToCompletion - this code will be called if the upload fails

        }, TaskContinuationOptions.NotOnRanToCompletion);
        task.ContinueWith(t =>
        {

            stream.Dispose();

        });
        return task;
   }

    private void irm_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
    {
        try
        {
            if (progress.Exception != null)
            {
                MessageBox.Show(progress.Exception.Message);  
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

下面是Fiddler Stack trace:

    **IF set ContentType = multipart/related; boundary="part_boundary"**

    REQUEST----------------------

    POST https://www.googleapis.com/upload/email/v2/users/abc%40example.com/mail?uploadType=resumable HTTP/1.1
    User-Agent: Email Migration google-api-dotnet-client/1.8.1.31685 (gzip)
    X-Upload-Content-Type: multipart/related; boundary="part_boundary"
    X-Upload-Content-Length: 570
    Authorization: Bearer ya29.eAAnFji2cG-AUB0AAABy4_I8vQFWmALqA9718djCOPL1Wwkl_iWk3WrK5PGoMQ
    Content-Type: application/json; charset=utf-8
    Host: www.googleapis.com
    Content-Length: 36
    Accept-Encoding: gzip, deflate
    Connection: Keep-Alive

    {"isInbox":true,"labels":["SANKET"]}


    RESPONSE------------

    HTTP/1.1 400 Bad Request
    Content-Type: application/json; charset=UTF-8
    Content-Length: 373
    Date: Sat, 06 Sep 2014 13:30:09 GMT
    Server: UploadServer ("Built on Aug 18 2014 11:58:36 (1408388316)")
    Alternate-Protocol: 443:quic

    {
     "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "badContent",
        "message": "Media type 'multipart/related; boundary=\"part_boundary\"' is not supported. Valid media types: [message/rfc822]"
       }
      ],
      "code": 400,
      "message": "Media type 'multipart/related; boundary=\"part_boundary\"' is not supported. Valid media types: [message/rfc822]"
     }
    }


    **if Set content-type = message/rfc822 then Two Requests** 

    1)  Request---------------

    POST https://www.googleapis.com/upload/email/v2/users/abc%40example.com/mail?uploadType=resumable HTTP/1.1
    User-Agent: Email Migration google-api-dotnet-client/1.8.1.31685 (gzip)
    X-Upload-Content-Type: message/rfc822
    X-Upload-Content-Length: 570
    Authorization: Bearer ya29.eAAnFji2cG-AUB0AAABy4_I8vQFWmALqA9718djCOPL1Wwkl_iWk3WrK5PGoMQ
    Content-Encoding: gzip
    Content-Type: application/json; charset=utf-8
    Host: www.googleapis.com
    Content-Length: 56
    Accept-Encoding: gzip, deflate
    Connection: Keep-Alive

    ‹ ]T ÿ«VÊ,öÌKʯP²*)*MÕQÊILJÍ)V²ŠV
    vôóv
    QŠ­ ']P±$   

    Response------------

    HTTP/1.1 200 OK
    Location: https://www.googleapis.com/upload/email/v2/users/abc@example.com/mail?uploadType=resumable&upload_id=AEnB2Ur3Nntr-eZT-K5-XoPDw2imWeMGylmY1SvOYr_ThmgDSkQBWe6gbxBy_fweqlRZPb_akwhwq2JZU9sy2NCWTvXdC6dr4A
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    Pragma: no-cache
    Expires: Fri, 01 Jan 1990 00:00:00 GMT
    Date: Sat, 06 Sep 2014 13:41:06 GMT
    Server: UploadServer ("Built on Aug 18 2014 11:58:36 (1408388316)")
    Content-Length: 0
    Content-Type: text/html; charset=UTF-8
    Alternate-Protocol: 443:quic


    2) Request ----------------------------

    PUT https://www.googleapis.com/upload/email/v2/users/abc@eaxmple.com/mail?uploadType=resumable&upload_id=AEnB2Ur3Nntr-eZT-K5-XoPDw2imWeMGylmY1SvOYr_ThmgDSkQBWe6gbxBy_fweqlRZPb_akwhwq2JZU9sy2NCWTvXdC6dr4A HTTP/1.1
    User-Agent: Email Migration google-api-dotnet-client/1.8.1.31685 (gzip)
    Authorization: Bearer ya29.eAAnFji2cG-AUB0AAABy4_I8vQFWmALqA9718djCOPL1Wwkl_iWk3WrK5PGoMQ
    Content-Range: bytes 0-569/570
    Host: www.googleapis.com
    Content-Length: 570
    Accept-Encoding: gzip, deflate

    --part_boundary
    Content-Type: message/rfc822

    Date: Wed, 03 Jan 2013 02:56:03 -0800
    From: admin@example.org
    To: liz@example.com
    Subject: Hello World!
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary=frontier

    --frontier
    Content-Type: text/plain; charset=windows-1252
    Content-Transfer-Encoding: 8bit

    And I think to myself... What a wonderful world!

    --frontier
    Content-Type: text/plain; name="test.txt"
    Content-Disposition: attachment; filename="test.txt"
    Content-Transfer-Encoding: base64

    SGVsbG8gQSE=

    --frontier--

    --part_boundary--


    Response :----------------

    HTTP/1.1 400 Bad Request
    Content-Type: application/json; charset=UTF-8
    Content-Length: 172
    Date: Sat, 06 Sep 2014 13:41:11 GMT
    Server: UploadServer ("Built on Aug 18 2014 11:58:36 (1408388316)")
    Alternate-Protocol: 443:quic

    {
     "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "invalid",
        "message": "Invalid Input"
       }
      ],
      "code": 400,
      "message": "Invalid Input"
     }
    }

1 个答案:

答案 0 :(得分:-1)

源代码可用here

我发现您已经在使用this,并且您已关注these

但是,你能否附上你所得到的确切错误以及小提琴堆栈追踪?