Category Archives: Internet/Network

Contains articles or source code related to Internet and Network programming.

Sending SMTP Email with Advanced Features in Visual Basic.NET | Part 1 – Adding Multiple Attachments

This is Part 1 of ‘Sending Email with Advanced Features’ which will show how you can send an email message and add not just one attachment, but pretty much as many attachments as you want. If you haven’t already, I recommend you check out this article I made on the basics of setting up and sending email using the System.NET.Mail SMTP feature in the DotNET Framework. The code I’m using is compatible with Visual Basic .NET 2005, VB 2008, and VB.NET 2010 since I’m using the classes under the System.NET.Mail namespace which is made available in the .NET 2.0, .NET 3.0, .NET 3.5, and the .NET 4.0 Frameworks. I am using SMTP and a Google GMail account to send the email message.

First of all to get started you will want a instance of a few classes. Below are the ones you’ll need to setup…

  • System.Net.Mail.SmtpClient
  • System.Net.Mail.MailMessage
  • System.Net.Mail.Attachment

Below are the variables I am using for the classes above..

Continue reading

Download Files with Advanced Features in VB 2008, Visual Basic 2010, and Higher

The .NET Framework has always made it easy to download files. But Dot.NET 2.0 added a new Async version of the DownloadFile feature. That basically means that if you use the DownloadFileAsync() sub it will download on a separate thread instead of consuming your application thread which makes your application appear frozen. If you used the regular DownloadFile and didn’t want your app’s thread tied up while downloading, you would have to spawn a new thread for the download. So Microsoft made it a little easier to download a file by adding the DownloadFileAsync method. This article and example works with Visual Basic 2008, VB 2010, and Visual Basic 2013 as well.

Updated 2015: I recently added some extra code to this article near the end. The code will add handlers for the DownloadProgress and DownloadCompleted events.


This is a simple article that will briefly highlight Downloading Files but the bulk of the code is in a VB.NET example program I made. There is way to much code to add here. The example application will not only show how to download files using the DownloadFileAsync method, but also how to add advanced features and capabilities that are not explicitly available in the DotNet 2.0/3.5/4.0 framework.

Below are some of the major features of the example project…

  • Download Progress updates like how many Bytes has been downloaded and the total size of the download.
  • Calculate the approximate download speed.
  • Calculate how long it will take the download to complete.
  • Keep track of how long the download has been active.
  • Keeping track of the Peak download speed.
  • Keeping track of the Average download speed.
  • Getting the current Downloading Status
  • Keeping track of the downloading progress and when its finished and more!

This is the base DownloadFileAsync sub I used below…

Public Sub DownloadFileAsync(ByVal address As System.Uri, ByVal fileName As String)

That method is part of the: System.Net.Webclient class. Webclient contains some very easy to use methods for uploading, downloading, and etc.

While the ‘MY’ feature has a default instance of DownloadFile under: My.Computer.Network, it unfortunately doesn’t have the DownloadFileAsync instance. So first of all to get started downloading you need to create a new instance of the Webclient class.

        '
        'Create a new instance of the Webclient class.
        Dim downloading As New Net.WebClient

You now have the DownloadFileAsync feature at your fingertips. Just simply call the subroutine while specifying the required parameters. Check the example code below…

        '
        'Will be used to setup the download url.
        Dim uriSource As Uri
        '
        'Setup a new Uniform Resource Identifier with the address to the file you want to download.
        'By the way the link in the code snippet below doesn’t work.
        uriSource = New Uri("http://www.vbcodesource.com/myFileToDownload.exe")

You can now get started performing the actual downloading.

        '
        'By using the Async version of DownloadFile your applications thread won’t be focused
        'on the download and thus you can still interact with your application. The only way
        'to get that feature with the regular DownloadFile method is by running it in a
        'seperate thread which is no longer required since an async version is availble.
        downloading.DownloadFileAsync(uriSource, "c:\MyDownloadedFile.exe")

That’s really all there is to it to get basic downloading support for your applications. If you want download progress, size, speed, time left till the download is finished and more, click this link to download the example project I mentioned earlier made with Visual Basic 2005. The program can be used in Visual Basic 2008 and Visual Basic 2010 as well.


I wanted to mention the Webclient class has two events you can use. They are Download Progress and Download Completed.

Public Event DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)

Public Event DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

All you have to do is Add Handler’s to each of those events to get reports each time data is received and when the file has finished downloading.

Edit: I added some simple code below on how to setup the two handlers.

Here are the Subroutines fired when the events are activated.

    'This will be fired whenever the DownloadProgress based event is hit.
    Sub getDownloadProgress(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)

        'Update the progress bar and percentage label.
        pbDownloadProgress.Value = e.ProgressPercentage

    End Sub

And

    'Will fire when the download has ended.
    Sub downloadHasEnded(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

        'Display the download has finished or was canceled.

        If e.Cancelled Then
            '
            'Show that the download had been canceled.
            Msgbox ("Status: Canceled")

        Else

            'Show that the download finished gracefully.
            Msgbox ("Status: Finished!")

        End If

    End Sub

This will set the above Subs to handle the events.

    'This sub will setup the event handlers for the program.
    Sub setupEventHandlers()

        'The getDownloadProgress Sub will fire whenever the DownloadAsync method updates the file
        'download status.
        AddHandler downloading.DownloadProgressChanged, AddressOf getDownloadProgress

        'The downloadHasEnded Sub is fired each time a download finishes.
        AddHandler downloading.DownloadFileCompleted, AddressOf downloadHasEnded

    End Sub

A picture of what the example application looks like


The application example highlights much more than the code in this article. So if you want, or ever needed to download file from the internet then this Link will get you a example project with full source code. Have Fun!

Jason

Revised: 2015

Free eBook: Patterns & Practices Application Architecture Guide 2.0

A while back Microsoft made available a new eBook for free called: Patterns and Pratices Application Architecture Guide 2.0

The download is about 3 megs of info and in the .pdf format. This book is should be very useful and helpful for .NET developers. Below are some of the features and Chapters in the free book.


Parts

Part I, Fundamentals
Part II, Design
Part III, Layers
Part IV, Archetypes

Foreword

  • Foreword by S. Somasegar
  • Foreword by Scott Guthrie

Chapters

  • Introduction
  • Architecture and Design Solutions At a Glance
  • Fast Track

 

Part I, Fundamentals

  • Chapter 1 – Fundamentals of Application Architecture
  • Chapter 2 – .NET Platform Overview
  • Chapter 3 – Architecture and Design Guidelines

   

Part II, Design

  • Chapter 4 – Designing Your Architecture
  • Chapter 5 – Deployment Patterns
  • Chapter 6 – Architectural Styles
  • Chapter 7 – Quality Attributes
  • Chapter 8 – Communication Guidelines

   

Part III, Layers

  • Chapter 9 – Layers and Tiers
  • Chapter 10 – Presentation Layer Guidelines
  • Chapter 11 – Business Layer Guidelines
  • Chapter 12 – Data Access Layer Guidelines
  • Chapter 13 – Service Layer Guidelines

   

Part IV, Archetypes

  • Chapter 14 – Application Archetypes
  • Chapter 15 – Web Applications
  • Chapter 16 – Rich Internet Applications (RIA)
  • Chapter 17 – Rich Client Applications
  • Chapter 18 – Services
  • Chapter 19 – Mobile Applications
  • Chapter 20 – Office Business Applications (OBA)
  • Chapter 21 – SharePoint Line-Of-Business (LOB) Applications

   

Appendix

  • Cheat Sheet – patterns & practices Pattern Catalog
  • Cheat Sheet – Presentation Technology Matrix
  • Cheat Sheet – Data Access Technology Matrix
  • Cheat Sheet – Workflow Technology Matrix
  • Cheat Sheet – Integration Technology Matrix

As you can see, this book can be very useful and helpful for those programming using the .NET framework. I recommend you download and check it out. Have fun!

Jason

More Microsoft “How Do I” Video Updates and Service Pack 1 for 2008

I just wanted to mention that Microsoft officially launched the RTM version Service Pack 1 for Visual Studio 2008 a couple weeks ago. It includes many fixes and some other features and improvements. WPF (Windows Presentation Foundation) performance has been greatly increased as well. Click Here to check out the Service Pack for VB 2008.

Also Microsoft has been adding new How Do I videos for Visual Basic 2008 the past couple of months it appears. Its definitely worth checking out. It looks like Microsoft added many newer videos working with Excel and Microsoft Office and services. Linq and data-based programming looks to be updated as well. Remember that MSDN has many 2005 based videos as well. Anyways, Click Here if you want to check out the full list of MSDN’s “How Do I” video series. Have Fun!

             Jason

Download a File from the Internet to the Hard Drive using VB 2005 and Higher

This will simply download a file from the internet to your harddrive path using Visual Basic .NET 2005 and Higher up to VB 2008, and VB 2010. This code snippet uses the Network class under the MY interface that was introduced in Visual Basic 2005.

        '
        'Download a specified file from the internet and put it in the specified directory.
        '
        My.Computer.Network.DownloadFile("http://www.webAdress.com/page.html", "c:\thePage.html")