Send Email with Attachment in VB from Windows Store Apps - XAML - UWP

In previous section, I introduced how to send HTML email. In this section, I will introduce how to add attachment to email in VB.

Introduction

To send an email with file attachment, we need to use AddAttachmentAsync method. This method can attach a file to the email message from local disk or a remote URL.

Note

Remarks: All of samples in this section are based on first section: Send email in A simple VB XAML Windows Store App project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[VB - Add attachment from local disk or remote URL]

The following example codes demonstrate how to send email with file attachments.

Note

To get the full sample projects, please refer to Samples section.

' Add EASendMail and Tasks Namespace
Imports EASendMail
Imports System.Threading.Tasks

Public NotInheritable Class MainPage
    Inherits Page

    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)

    End Sub

    Private Async Function btnSend_Click(sender As Object, e As RoutedEventArgs) _
        As Task Handles btnSend.Click
        btnSend.IsEnabled = False
        Await Send_Email()
        btnSend.IsEnabled = True

    End Function

    Private Async Function Send_Email() As Task
        Dim Result As String = ""
        Try

            Dim oMail As New SmtpMail("TryIt")

            ' Set sender email address, please change it to yours
            oMail.From = New MailAddress("test@emailarchitect.net")

            ' Add recipient email address, please change it to yours
            oMail.To.Add(New MailAddress("support@emailarchitect.net"))

            // Set email subject
            oMail.Subject = "test HTML email from VB XAML project"

            // Set Html body
            oMail.HtmlBody = "<font size=5>This is</font> <font color=red><b>a test</b></font>"

        ' get a file path from PicturesLibrary,
            ' to access files in PicturesLibrary, you MUST have "Pictures Library" checked in
            ' your project -> Package.appxmanifest -> Capabilities
            Dim file As Windows.Storage.StorageFile =
                Await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("test.jpg")

            Dim attfile As String = file.Path
            Dim oAttachment As Attachment = Await oMail.AddAttachmentAsync(attfile)

            ' if you want to add attachment from remote URL instead of local file.
            ' Dim attfile As String = "http://www.emailarchitect.net/test.jpg"
            ' Dim oAttachment As Attachment = Await oMail.AddAttachmentAsync(attfile)

            ' you can change the Attachment name by
            ' oAttachment.Name = "mytest.jpg"

            ' Your SMTP server address
            Dim oServer As New SmtpServer("smtp.emailarchitect.net")

            ' User and password for ESMTP authentication
            oServer.User = "test@emailarchitect.net"
            oServer.Password = "testpassword"

            ' If your SMTP server requires TLS connection on 25 port, please add this line
            ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

            ' If your SMTP server requires SSL connection on 465 port, please add this line
            ' oServer.Port = 465
            ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

            Dim oSmtp As New SmtpClient()

            Await oSmtp.SendMailAsync(oServer, oMail)
            Result = "Email was sent successfully!"

        Catch ep As Exception
            Result = String.Format("Failed to send email with the following error: {0}", ep.Message)
        End Try

        ' Display Result by Diaglog box
        Dim dlg As New Windows.UI.Popups.MessageDialog(Result)
        Await dlg.ShowAsync()
    End Function
End Class

Next Section

At next section I will introduce how to add embedded images/pictures to email message.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.