Found the problem, and its not StreamWriter not closing itself after usings
.
Chris Sinclair is right about DateTime
not guaranteeing an unique file name. If your for-loop is short (therefore, fast), you can end up with duplicate names, which is what happened in my case.
For 5 emails, _txtName
generated 5 identical file names, meaning I ended up with one file in the end since StreamWriter overwrites by default.
Also, I forgot to use x.Attachments.Dispose();
at the end of each loop.
So when it re-iterated, x.Attachments.add()
is still trying attach the same file (upload time) while StreamWriter
begins to write to the same file cause _txtName
generated duplicate names due to DateTime
being slower than the for-loop.
TL;DR: The for-loop is too fast too furious.