3
votes

I am trying to get the message using Gmail API's. I am able to fetch the messages which are smaller in size. But when try to get large size message, it throws exception as 'Task was cancel'. As per google documentation I tried to get message as RAW as well as FULL formats. Also I tried with increased timeout for request. But having same result.

Is there any other way by which I can read large messages?

Here is my sample code:

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

Thanks in advance.

EDIT: Comlpete LOG

-----------------------------------------------------------------------------------------MAIN EXEPTION------------------------------------------------

Error while copying content to a stream.

at System.Net.Http.HttpContent.d__49.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClient.d__58.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Apis.Requests.ClientServiceRequest1.<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.ClientServiceRequest1.Execute()

-----------------------------------------------------------------------------------------INNER EXEPTION------------------------------------------------

Received an unexpected EOF or 0 bytes from the transport stream.

at System.Net.GZipWrapperStream.EndRead(IAsyncResult asyncResult)
at System.IO.Stream.<>c.b__43_1(Stream stream, IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.FromAsyncTrimPromise1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.IO.Stream.d__27.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.StreamToStreamCopy.<>c.b__1_0(Task completed, Object innerSource) at System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpContent.d__49.MoveNext()

1
Please edit your question and include your full error message and please define large.DaImTo
@DalmTo Large email is having size more than 5MB. If the message size is in KB's its able to fetch the dataMayuresh
@Mayuresh - Are you trying to send any media (.png, .jpg or video) or any file in mail. or it is simple text onlyUbiquitous Developers
I am not trying to send the message. I am trying to read the message data. and message contains attachments can be of any file format.Mayuresh
Try using Fiddler to check the raw response. Could be you need to enable Automatic Decompression for GZip using an HttpClientHandler or possibly you should start processing the request after just reading the headersste-fu

1 Answers

2
votes
MessageGetRequestObj.Service.HttpClient.Timeout = new TimeSpan(0, 5, 0);

this is likely your issue. Looks like you're setting your HTTP client to timeout after 5 (seconds?) inactivity. You should either increase that timeout significantly or disable timeout entirely.