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.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()-----------------------------------------------------------------------------------------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.FromAsyncTrimPromise
1.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()