使用适用于Gmail的.NET api阅读大尺寸电子邮件

时间:2018-11-22 04:38:34

标签: c# gmail-api google-api-dotnet-client gsuite

我正在尝试使用Gmail API来获取消息。我能够提取较小的消息。但是,当尝试获取大尺寸消息时,由于“任务已取消”,它将引发异常。 根据谷歌documentation,我尝试以RAW以及FULL格式获取消息。我也尝试增加超时请求。 但结果相同。

还有其他阅读大邮件的方式吗?

这是我的示例代码:

try 
{
    GmailServiceObj = new GmailService(new BaseClientService.Initializer()
                                {
                                    ApplicationName = _appname,
                                    HttpClientInitializer = _userCredentials
                                }); 
    UsersResource.MessagesResource.GetRequest MessageGetRequestObj;
    MessageGetRequestObj= GmailServiceObj.Users.Messages.Get(UserEmailID, ItemID);        
    MessageGetRequestObj.Service.HttpClient.Timeout = new TimeSpan(0, 5, 0);        
    MessageGetRequestObj.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;        
    var _message = MessageGetRequestObj.Execute(); // throws exception
}
catch(Exception e)
{   //The request was aborted: The request was canceled.
    // trying to read same message in parts
    MessageGetRequestObj.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
    var _message = MessageGetRequestObj.Execute();
    string res = Readparts(Emailobj.Payload.Parts, ItemID);

}

private String Readparts(IList<MessagePart> parts, string msgid)
{
    StringBuilder mime = new StringBuilder();
    try
    {
        foreach (var part in parts)
        {

            try
            {
                if (part != null && part.Body != null)
                    mime.Append(part.Body.Data);
                if (part.Body != null && part.Body.AttachmentId != null)
                {
                    var resource = GmailServiceObj.Users.Messages.Attachments.Get("me", msgid, part.Body.AttachmentId);
                    MessagePartBody attachPart = resource.Execute(); // Throws exception 

                    // Converting from RFC 4648 base64 to base64url encoding
                    // see http://en.wikipedia.org/wiki/Base64#Implementations_and_history
                    String attachData = attachPart.Data.Replace('-', '+');
                    attachData = attachData.Replace('_', '/');
                    mime.Append(attachData);
                }
                if (part.Parts != null && part.Parts.Count > 0)
                {
                    mime.Append(Readparts(part.Parts, msgid));
                }
            }
            catch (Exception er) { }
        }
    }
    catch (Exception ex)
    {}
    return mime.ToString();
}

谢谢。

编辑:Comlpete日志

  

-------------------------------------------------- ------------------------------------------主要表现------ ------------------------------------------

     

将内容复制到流时出错。

     在

  System.Net.Http.HttpContent.d__49.MoveNext()   ---从上一个引发异常的位置开始的堆栈跟踪-   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   System.Net.Http.HttpClient.d__58.MoveNext()   ---从上一个引发异常的位置开始的堆栈跟踪-   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   Google.Apis.Requests.ClientServiceRequest 1.<ExecuteUnparsedAsync>d__33.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Google.Apis.Requests.ClientServiceRequest 1.Execute()

     

-------------------------------------------------- ------------------------------------------内部表现------ ------------------------------------------

     

从传输流中接收到意外的EOF或0个字节。

     

在System.Net.GZipWrapperStream.EndRead(IAsyncResult asyncResult)
  在System.IO.Stream。<> c.b__43_1(Stream stream,   IAsyncResult asyncResult)   System.Threading.Tasks.TaskFactory 1.FromAsyncTrimPromise 1.Complete(TInstance   thisRef,Func`3 endMethod,IAsyncResult asyncResult,布尔值   需要同步)   ---从上一个引发异常的位置开始的堆栈跟踪-   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务),位于System.IO.Stream.d__27.MoveNext()   ---从上一个引发异常的位置开始的堆栈跟踪-   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   System.Net.Http.StreamToStreamCopy。<> c.b__1_0(任务   已完成,对象innerSource)在   System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke()在   System.Threading.Tasks.Task.Execute()   ---从上一个引发异常的位置开始的堆栈跟踪-   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   System.Net.Http.HttpContent.d__49.MoveNext()

1 个答案:

答案 0 :(得分:2)

Row(
    // mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      RaisedButton(
          child: Text('data'), 
          onPressed: () {}
      ),
      Expanded(child: Text(s))
    ],
  )

这可能是您的问题。好像您将HTTP客户端设置为在闲置5(秒)后超时。您应该大幅增加该超时时间,或者完全禁用超时。