This is where debugging comes in to the equation; set a
Breakpoint at the beginning of the
For Each
loop and see what you have for values:
1. File_path
2. File_Name
3. attachmentFile
Generally when I am building emails with attachments, I do it in this order
1. Define server and credentials
2. Create an instance of the SMTP Client, overloaded with server & credentials
3. Define the the email basic email information (from, to, subject, body)
4. Create an instanced of the MailMessage, overloaded with the basic email info
5. Attachments- a routine all of their own:
5-1. Create the attachment object, overloaded with filename AND content-type (e.g. MediaTypeNames.Application.Octet)
5-2. Create a ContentDisposition from the attachment
5-3. Populate the ContentDisposition with dates, name, size, and type
5-4. Add the attachment to the MailMessage.
I would also use
using
blocks for cleaning up the SMTP and Mail objects to properly dispose of resources.
A good reference from a well trusted person here in C# as a routine on it's own:
Sending an Email in C# with or without attachments: generic routine.[
^]