Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an CheckedListbox (imported from mysql Database) that lists all my folders where my files are saved. And what I need to do is for every checked item in the CheckListbox, to search the folder and then attach the file from that folder to a Mail.

What I have done is to create an Email and to send it with only one attachment. Just with the: "e_mail. Attachments. Add". The Mail will be generated and that found file will be attached and send.

But when I do that in a FOR EACH LOOP then no file will be selected and added to my Mail. The Mail will be generated and send without any errors, but there are no attached files.

Private Sub SendMail()
    Dim Smtp_Server As New SmtpClient
    Dim e_mail As New MailMessage()
    Smtp_Server.UseDefaultCredentials = False
    Smtp_Server.Credentials = New Net.NetworkCredential("MAIL", "PASS")
    Smtp_Server.Port = 587
    Smtp_Server.EnableSsl = True
    Smtp_Server.DeliveryMethod = SmtpDeliveryMethod.Network
    Smtp_Server.Host = "HOST"
    If MonthTextBox.Text = "" Or MonthTextBox.ForeColor = Color.Silver Then
        MsgBox("Input month please", Title:="MO Text Box")
    Else
        Dim MOTextValue As String = MonthTextBox.Text
        Dim nowYear As Integer = Date.Now.Year

        For Each itemchecked As DataRowView In CheckedListBoxWO.CheckedItems
            Dim File_path As String = "C:\Test\" & itemchecked.Item(1) & "\" & nowYear & "\" & MOTextValue & "\"
            Dim File_Name As String = Dir("C:\Test\" & itemchecked.Item(1) & "\" & nowYear & "\" & MOTextValue & "\File " & MOTextValue & "*.xlsx")
            Dim attachmentFile As Net.Mail.Attachment = New Net.Mail.Attachment(File_path & File_Name)
            e_mail.Attachments.Add(attachmentFile)
        Next

        Dim sb As New System.Text.StringBuilder
        sb.AppendLine("Hello,<br />")
        sb.AppendLine("<br />")            
        sb.AppendLine("Test Attachment,<br />")           
        sb.AppendLine("<br />")
        sb.AppendLine("------------------------------------------<br />")            
        e_mail.From = New MailAddress("MY MAIL")
        e_mail.To.Add("EMAIL")
        e_mail.Subject = ("TESTFILE - " & MOTextValue & "")
        e_mail.SubjectEncoding = System.Text.Encoding.UTF8
        e_mail.IsBodyHtml = True
        e_mail.Priority = MailPriority.Normal
        e_mail.Body = sb.ToString()
        e_mail.BodyEncoding = System.Text.Encoding.UTF8
        Smtp_Server.Send(e_mail)
    End If
    MsgBox("Mails was sent", Title:="Mail")
End Sub


What I have tried:

Like I told with this code the Mail will be created and send, but there is no Attachment file in it. If I do it without the "FOR EACH"... loop, then one File will be added without a problem. But I need to add more files, actually for every checked Item in the CheckedListBox my program should search that folder and if there is a file than attach it in the Mail, and look in the other folder and so on...
Posted
Updated 27-Aug-19 7:35am

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.[^]
 
Share this answer
 
Dir is a throw-back to VB6, and should be avoided in favour of the System.IO classes.

Try:
VB.NET
For Each itemchecked As DataRowView In CheckedListBoxWO.CheckedItems
    Dim folderPath = System.IO.Path.Combine("C:\Test\", itemchecked.Item(1), nowYear, MOTextValue)
    For Each filePath As String In System.IO.Directory.EnumerateFiles(folderPath, "File " & MOTextValue & "*.xlsx")
        Dim attachmentFile As New Net.Mail.Attachment(filePath)
        e_mail.Attachments.Add(attachmentFile)
    End If
Next
If it still doesn't work, then you'll need to debug your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900