2

I have managed to send an email with an attachment that is in a specific location and named specifically ("C:\New\Log.txt")

However, I want to be able to send an email with all the attachments in a given folder no mater what they are called. All variable settings are configured elsewhere in the project using my.settings and i would like similar for the folder destination i.e. my.settings.fileloc1 for the location of the files

Below is my current code. I'm pretty sure it will involve getfiles, but am running on empty....please help!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New  _
        Net.NetworkCredential(My.Settings.SMTPuser, My.Settings.SMTPuser)
        SmtpServer.Port = My.Settings.SMTPPort
        SmtpServer.Host = My.Settings.SMTPHost
        mail = New MailMessage()
        mail.From = New MailAddress(My.Settings.from)
        mail.To.Add(My.Settings.recipient)
        mail.Subject = My.Settings.subject
        mail.Body = My.Settings.body
        Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment("C:\New\Log.txt")
        '^^The above needs to be an actual file
        '^^I want it to select all files in a given folder and attach them!
        mail.Attachments.Add(Attach)
        SmtpServer.Send(mail)
        MsgBox("Mail Sent")
    Catch ex As Exception
        MsgBox("Email Settings are either incomplete or incorrect" & vbNewLine & "Please see below details:" & vbNewLine & vbNewLine & ex.ToString)
    End Try

End Sub

Thanks for anything you can come up with :)

2 Answers 2

2

Try a For Each loop looking for all files in System.IO.Directory.GetFiles()

Sign up to request clarification or add additional context in comments.

Comments

0
' ...
For Each filePath As String In Directory.GetFiles(My.Settings.FileLoc1)
    Dim Attach As New Net.Mail.Attachment(filePath)
    mail.Attachments.Add(Attach)
Next
SmtpServer.Send(mail)
' ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.