C# 使用.NETAPI for Gmail阅读大型电子邮件
我正在尝试使用Gmail API获取消息。我能够获取较小的消息。但当尝试获取大尺寸消息时,它抛出异常“TaskwasCancel”。 根据谷歌的说法,我试图获得原始和完整格式的信息。我还尝试增加请求的超时时间。 但结果是一样的 有没有其他方法可以让我阅读大型邮件 以下是我的示例代码:C# 使用.NETAPI for Gmail阅读大型电子邮件,c#,gmail-api,google-api-dotnet-client,google-workspace,C#,Gmail Api,Google Api Dotnet Client,Google Workspace,我正在尝试使用Gmail API获取消息。我能够获取较小的消息。但当尝试获取大尺寸消息时,它抛出异常“TaskwasCancel”。 根据谷歌的说法,我试图获得原始和完整格式的信息。我还尝试增加请求的超时时间。 但结果是一样的 有没有其他方法可以让我阅读大型邮件 以下是我的示例代码: try { GmailServiceObj = new GmailService(new BaseClientService.Initializer()
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();
}
试试看
{
Gmail服务obj=新的Gmail服务(新的BaseClientService.Initializer()
{
ApplicationName=\u appname,
HttpClientInitializer=\u用户凭据
});
UsersResource.MessagesResource.GetRequest MessageGetRequestObj;
MessageGetRequestObj=GmailServiceObj.Users.Messages.Get(UserEmailID,ItemID);
MessageGetRequestObj.Service.HttpClient.Timeout=新的时间跨度(0,5,0);
MessageGetRequestObj.Format=UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;
var _message=MessageGetRequestObj.Execute();//引发异常
}
捕获(例外e)
{//请求已中止:请求已取消。
//尝试在部分中读取相同的消息
MessageGetRequestObj.Format=UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
var_message=MessageGetRequestObj.Execute();
string res=Readparts(Emailobj.Payload.Parts,ItemID);
}
私有字符串读部件(IList部件、字符串msgid)
{
StringBuilder mime=新的StringBuilder();
尝试
{
foreach(var零件中的零件)
{
尝试
{
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();//引发异常
//从RFC 4648 base64转换为base64url编码
//看http://en.wikipedia.org/wiki/Base64#Implementations_and_history
字符串attachData=attachPart.Data.Replace('-','+');
attachData=attachData.Replace(“”,“/”);
mime.Append(attachData);
}
如果(part.Parts!=null&&part.Parts.Count>0)
{
mime.Append(Readparts(part.Parts,msgid));
}
}
捕获(异常er){}
}
}
捕获(例外情况除外)
{}
返回mime.ToString();
}
提前谢谢
编辑:Comlpete日志
-----------------------------------------------------------------------------------------主要解释------------------------------------------------
将内容复制到流时出错
在
System.Net.Http.HttpContent.d__49.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
System.Net.Http.HttpClient.d_u58.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Google.api.Requests.ClientServiceRequest1.d_u33.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
Google.api.Requests.ClientServiceRequest
1.Execute()
-----------------------------------------------------------------------------------------内训诂------------------------------------------------
从传输流接收到意外的EOF或0字节
在System.Net.GZipWrapperStream.EndRead(IAsyncResult asyncResult)在System.IO.Stream.c.b__43_1(Stream, IAsyncResult(异步结果)位于 System.Threading.Tasks.TaskFactory
1.FromAsyncTrimPromise
1.Complete(t状态
thisRef,Func`3 endMethod,IAsyncResult asyncResult,布尔型
要求(同步)
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)位于System.IO.Stream.d_u27.MoveNext()处
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
System.Net.Http.StreamToStreamCopy.c.b__1_0(任务
已完成,对象内部源代码)位于
System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke()位于
System.Threading.Tasks.Task.Execute()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
System.Net.Http.HttpContent.d__49.MoveNext()
这可能是你的问题。看起来您正在将HTTP客户端设置为5(秒?)不活动后超时。您应该显著增加超时时间或完全禁用超时时间。请编辑您的问题并包含完整的错误消息,然后定义大的。@DalmTo-large电子邮件
MessageGetRequestObj.Service.HttpClient.Timeout = new TimeSpan(0, 5, 0);