Files - Folder attachment with VB.Net

~OSD~ 2,196 Reputation points
2020-12-16T13:40:45.54+00:00

Hi,

I am using following sample code to send email to Gmail, working fine so far. However, I would like to add email attachment(s). For example, everything under the C:\Users\%UserName%\Attachments should be emailed, is it possible?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Mail As New MailMessage
        Mail.Subject = "test APP email"
        Mail.To.Add("destEmailID@gmail.com")
        Mail.From = New MailAddress("scrEmailIDa@gmail.com")
        Mail.Body = "Hello from myEmail App "
        Dim SMTP As New SmtpClient("smtp.gmail.com")
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential("myEmailID@gmail.com", "myGmailPassword")
        SMTP.Port = "587"
        '    File Attachments / Directory Path >> %UserName%\Attachment
        SMTP.Send(Mail)
    End Sub
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,741 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2020-12-16T14:31:56.727+00:00

    Hi
    You could do this (using your own path of course)

     For Each s As String In IO.Directory.GetFiles("C:\Users\lesha\Desktop\New folder", "*", IO.SearchOption.AllDirectories)
     Mail.Attachments.Add(New Attachment(s))
     Next
    

    EDIT: added filter and subdirectories

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,441 Reputation points
    2020-12-16T16:54:44.177+00:00

    Hello @~OSD~

    You might also consider MailKit installed via NuGet as the Microsoft mail classes are not recommended by Microsoft
    48842-a1.png

    Simple Example

    Imports MailKit  
    Imports MailKit.Security  
    Imports MimeKit  
    Imports MimeKit.Utils  
      
    Public Class Operations  
        Public Shared Async Sub Example1()  
            Dim message = New MimeMessage()  
      
            message.From.Add(New MailboxAddress("Joey", "joey@friends.com"))  
            message.To.Add(New MailboxAddress("Alice", "alice@wonderland.com"))  
      
            message.Subject = "Hello friend"  
      
            Dim builder = New BodyBuilder()  
            Dim image = builder.LinkedResources.Add("C:\Users\Joey\Documents\Selfies\selfie.jpg")  
      
            image.ContentId = MimeUtils.GenerateMessageId()  
      
      
            builder.HtmlBody =  
                $"<p>Hey Alice,<br>  
    <p>What are you up to this weekend? Monica is throwing one of her parties on  
    Saturday and I was hoping you could make it.<br>  
    <p>Will you be my +1?<br>  
    <p>-- Joey<br>  
    <center><img src=""cid:{image.ContentId}""></center>"  
            builder.Attachments.Add("C:\Users\Joey\Documents\party.ics")  
      
            message.Body = builder.ToMessageBody()  
      
            Using smtp = New Net.Smtp.SmtpClient()  
      
                AddHandler smtp.MessageSent, AddressOf MessageSent  
      
                Await smtp.ConnectAsync("smtp.gmail.com", 587, SecureSocketOptions.SslOnConnect)  
      
                Await smtp.AuthenticateAsync("Username", "Password")  
                Await smtp.SendAsync(message)  
                Await smtp.DisconnectAsync(True)  
      
            End Using  
      
        End Sub  
      
        Private Shared Sub MessageSent(sender As Object, e As MessageSentEventArgs)  
            Console.WriteLine(e.Response)  
        End Sub  
    End Class  
    

    Notes

    You can create classes to represent email messages (and more but let's keep it simple)

    Public Class EmailAddress  
        Public Property Name() As String  
        Public Property Address() As String  
    End Class  
    Public Class EmailMessage  
        Public Sub New()  
            ToAddresses = New List(Of EmailAddress)()  
            FromAddresses = New List(Of EmailAddress)()  
        End Sub  
      
        Public Property ToAddresses() As List(Of EmailAddress)  
        Public Property FromAddresses() As List(Of EmailAddress)  
        Public Property Subject() As String  
        Public Property Content() As String  
        Public Property Attachments() As AttachmentCollection  
    End Class  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.