System.Net.Http.HttpRequestException: 'Error while copying content to a stream.'

0

I am using the Gmail API and Im trying to send an email with a PDF attachment. The PDF attachment includes promo emails from my inbox. Everything runs fine until I get try to send a message with the attachment. I get this error saying that the connection was aborted by the software in my host machine.

System.Net.Http.HttpRequestException
  HResult=0x80131500
  Message=Error while copying content to a stream.
  Source=mscorlib
  StackTrace:
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Google.Apis.Requests.ClientServiceRequest`1.<ExecuteUnparsedAsync>d__30.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Google.Apis.Requests.ClientServiceRequest`1.Execute()
   at Newspaper.Newspaper.SendEmail(GmailService gmailservice, MimeMessage msg, String id) in F:\Learning\CSharp\Projects\EasyMail\Program.cs:line 233
   at Newspaper.Newspaper.Main(String[] args) in F:\Learning\CSharp\Projects\EasyMail\Program.cs:line 68

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.

Inner Exception 2:
SocketException: An established connection was aborted by the software in your host machine

Here is a snippet of the code:

static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "admin",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }


            var service = new GmailService(new BaseClientService.Initializer()
            {   
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,

            });




            CreateFile();
            var msgList =  GetMessageList(service);
            // gets content of all messages and puts it into a HTML file
            GetAllMessages(msgList); 

            ToPDF(newsfname);

            SendEmail(service, CreateEmail(credential.UserId), credential.UserId);
        }


// connection aborts here
static void SendEmail(GmailService gmailservice, MimeMessage msg, string id) {            
            Message message = new Message();
            message.Raw = Encode(msg.ToString());
            gmailservice.Users.Messages.Send(message, id).Execute();

        }

What is causing it to be aborted? and how ca

c#
.net
gmail
gmail-api
asked on Stack Overflow Apr 7, 2020 by Corex

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0