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.
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.
Start Visual Basic and create a new Windows Forms application. You may name it anything you like. Design your Form to resemble Figure 1:
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.
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:firstname.lastname@example.org") 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:
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:email@example.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
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!