代码之家  ›  专栏  ›  技术社区  ›  Mayuresh

使用电子邮件阅读大容量电子邮件。Gmail的NetAPI

  •  3
  • Mayuresh  · 技术社区  · 6 年前

    我正在尝试使用Gmail API获取消息。我能够获取较小的消息。但当试图获取大尺寸消息时,它会抛出异常,如“Task was cancel”。 根据谷歌 documentation 我试图获得原始和完整格式的信息。我还尝试增加请求超时时间。 但结果是一样的。

    有没有其他方法可以让我阅读大量信息?

    以下是我的示例代码:

    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日志

    -----------------------------------------------------------------------------------------主要解释------------------------------------------------

    将内容复制到流时出错。

    在 系统网Http。HttpContent。d__49。下一步 ---来自引发异常的前一个位置的堆栈结束跟踪——在 系统运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务) 任务)在 系统运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务 任务)在 系统网Http。HttpClient。d__58。下一步 ---来自引发异常的前一个位置的堆栈结束跟踪——在 系统运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务) 任务)在 系统运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务 任务)在 谷歌。API。请求。客户服务请求 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.执行

    -----------------------------------------------------------------------------------------内训诂------------------------------------------------

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

    在系统中。网GZipWrapperStream。EndRead(IAsyncResult异步结果)
    在系统中。伊奥。小溪<>c、 b_u 43_u1(溪流, IAsyncResult(结果)位于 系统穿线。任务。任务工厂 1.FromAsyncTrimPromise 1.完成(t) thisRef,Func`3 endMethod,IAsyncResult asyncResult,布尔型 要求(同步) ---来自引发异常的前一个位置的堆栈结束跟踪——在 系统运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务) 任务)在 系统运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务 ---来自引发异常的前一个位置的堆栈结束跟踪——在 系统运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务) 任务)在 系统运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务 任务)在 系统网Http。流到流拷贝<>c、 b__1_0(任务 已完成,对象内部源代码)位于 系统穿线。任务。ContinuationTaskFromTask。InnerInvoke()位于 系统穿线。任务。任务执行 ---来自引发异常的前一个位置的堆栈结束跟踪——在 系统运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务) 任务)在 系统运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务 任务)在

    0 回复  |  直到 6 年前
        1
  •  2
  •   Jay Lee    5 年前
    MessageGetRequestObj.Service.HttpClient.Timeout = new TimeSpan(0, 5, 0);
    

    这可能是你的问题。看起来您正在将HTTP客户端设置为5秒后超时(秒?)不活动。您应该显著增加超时或完全禁用超时。