Using VB to Determine Your Default Browser and Default Mail Client

Introduction

Hello and welcome to today's interesting article! A frequent question posted on the CodeGuru forums is: "How do I send a mail with the default mail client?" And another interesting question posted is: "How do I open the user's default web browser?" Today, I will answer both of these questions for you.

Default Mail Client and Default Web Browser

Before I start with the project, I must explain a couple of things for the newbies. A default program refers to the chosen program by the user to achieve certain goals. For example: My default word processor is Microsoft Word, my default mail client is Microsoft Outlook, and my default web browser is Firefox. Now, it is always good practise to utilise the user's default program from within your programs. It is basically the norm and it rounds off your application properly; plus, there's the fact that it is more professional and your application will be easier to accept and use.

Not making use of user's default programs, and even default settings, will let users frown upon your app and make it more difficult to use. It is impossible to easily know exactly what programs users have installed on their system. Imagine if your application tries to open an application that is not installed on the user's system. This will end up breaking your application as well as break the user's trust in your application.

Our Application

Now that you have a good understanding of what a default program is, let's do a small example on how to use the user's default mail client to send an email, and use the user's default web browser to navigate to a web page.

Design

Start Visual Basic and create a new Windows Forms application. You may name it anything you like. Design your Form to resemble Figure 1:

DBrowser1
Figure 1: Our design

You also may name all the objects on the form anything you like, but keep in mind that my object names may differ from yours.

Let's move on to the code.

Code

Default Web Browser

First, let me cover how to use the default web browser. Add the following code behind the button labelled "Browser":

Private Sub Button1_Click(sender As Object, e _
      As EventArgs) Handles Button1.Click

   Dim BrowseProc As New Process   'declare a new process
   'open default browser client
   BrowseProc.StartInfo.FileName = "http://www.codeguru.com"
   BrowseProc.StartInfo.UseShellExecute = True
   BrowseProc.StartInfo.RedirectStandardOutput = False
   BrowseProc.Start()
   BrowseProc.Dispose()

End Sub

By the looks of it, the preceding code doesn't look too complicated. Well, it isn't! The first line of code creates a new process object. This object is ultimately responsible for starting the associated process, or program. The next line of code specifies a filename for the process object. You may be asking: "Aren't we dealing with web pages instead of files?" Yes, but remember that the process object usually deals with applications dealing with files and that a web page is technically still just a file.

The magic happens with the third line of code. Here, I set the UseShellExecute property to True. This tells the compiler that it must use the default program associated with the supplied file type on the second line of code. The remaining lines start the process and remove it from memory.

If you were to run your application now, you will be able to launch your default web browser with a click of a button!

Default Mail Client

Launching the user's default mail client is almost as easy, but obviously there is a little more work involved. Add the following function to your code:

Private Function SendMail(ByVal MailtoStr As String) _
      As Boolean

   Dim MailP As New Process   'declare a new process
   'open default mail client
   MailP.StartInfo.FileName = MailtoStr
   MailP.StartInfo.UseShellExecute = True
   MailP.StartInfo.RedirectStandardOutput = False
   MailP.Start()
   MailP.Dispose()

End Function

As you can see, the code is basically the same except for the fact that it is now contained inside a function. Now, let's make use of this function:

Private Sub Button2_Click(sender As Object, e _
      As EventArgs) Handles Button2.Click

   Dim strSubject As String   'subject title
   Dim strBody As String   'Body of message
   Dim strSubject1 As String   'Subject line
   Dim strBody2 As String   'Body of Message

   'store subject in variable
   strSubject = txtSubject.Text
   strBody = txtBody.Text   'store body in variable

   Dim mail As New System.Text.StringBuilder

   strSubject1 = "?subject=" & strSubject
   strBody2 = "&body=" & strBody

   'email address
   mail.Append("mailto:user@codeguru.com")

   mail.Append(strSubject1)   'subject
   mail.Append(strBody2)   'body of message
   SendMail(mail.ToString)

End Sub

In the previous piece of code, I set up an email message, complete with a subject and a recipient address. Lastly, I simply use the SendMail function to open up the email in the default mail client ready to be sent, as shown in Figure 2:

DBrowser2
Figure 2: Email opened in the default mail client

Just as a side note: You may have noticed that the preceding code didn't include anything concerning adding a mail attachment. If you modify the code to the following, you will be able to attach a file as well:

Private Sub Button2_Click(sender As Object, _
      e As EventArgs) Handles Button2.Click

   Dim strSubject As String   'subject title
   Dim strBody As String   'Body of message
   Dim strSubject1 As String   'Subject line
   Dim strBody2 As String   'Body of Message

   'store subject in variable
   strSubject = txtSubject.Text
   strBody = txtBody.Text   'store body in variable

   Dim mail As New System.Text.StringBuilder

   strSubject1 = "?subject=" & strSubject
   strBody2 = "&body=" & strBody

   'email address
   mail.Append("mailto:user@codeguru.com")

   mail.Append(strSubject1)   'subject
   mail.Append(strBody2)   'body of message

   mail.Append("&Attach=""""C:\mailattach.txt""""")
   mail.Append("&Attach=" & Chr(34) & _
      "C:\mailattach.txt" & Chr(34))

   SendMail(mail.ToString)
End Sub

Conclusion

As you can see, it is quite straightforward to open up a user's default application. I hope you have learned something today! Until next time, cheers!



Related Articles

Downloads

Comments

  • Default Mail Client with attchment

    Posted by sandhya on 04/28/2016 10:22am

    Hi, Thank you for this article. I downloaded and run your application. I am able to open outlook. But when I add the attachment it throws an error " The command line argument is not valid. Verify the switch you are using". Can you please tell me how to open outlook with attachment. Is there is something I am missing? Thanks, Sandhya

    Reply
  • Problems sending Email

    Posted by Herman Tabbert on 03/06/2016 04:00am

    Hoi Hannes, Was looking for a long time for this to send Email with attachment. But, when running i get a error on the line: Dim mail As New System.Text.StringBuilder Compile error: User defined type not defined In the function Sendmail thes two lines are red: MailP.Start() MailP.Dispose() Can you help me? Regards, Herman Tabbert

    Reply
  • VB Developer

    Posted by Henry on 02/12/2016 09:30am

    HI I downloaded and tried your app. When I click Send, I get this error The command line argument is not valid. Verify the switch you are using. Can you identify the problem? Thanks H

    Reply
  • Library

    Posted by Victoria on 05/11/2015 12:19pm

    Hi Hannes, Thank you for this article. If I were attempting to use this technique from within MS Office 2010, do you know what library I would need to set a reference to? Or is it not possible? Thank you!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date