Opening Default Email Client  
Author Message
Pierce Blaylock





PostPosted: Visual Basic Language, Opening Default Email Client Top

Hi, I'm developing an application using Visual Basic 2005 and I'm just wondering how I go about opening a new email using the default email client

Basically, my application has a section where the user can enter email addresses. I want the user to be able to click on any of the email addresses and have the default email client set in Windows (such as Outlook or Outlook Express for example) open up a New Message window with the To field already filled in with the email address the user clicked on.

I'm not sure if this is a Visual Basic related question or not, but any help would be much appreciated. Thanks.



Visual Basic7  
 
 
ahmedilyas





PostPosted: Visual Basic Language, Opening Default Email Client Top

this could possibly be bad practice but this should work:

 



Dim theStringBuilder as new System.Text.StringBuilder()
")
theStringBuilder.Append(" subject=theSubject")
theStringBuilder.Append("&body=your message here")
 
System.Diagnostics.Process.Start(theStringBuilder.ToString())

 

 

does this help

 

just for your information - you are aware that you can send emails within your application using the System.Net.Mail namespace

you would of course need an SmtpServer to send the email through using the MailMessage to create the email message object and also any appropriate authentication to the SmtpService



 
 
Pierce Blaylock





PostPosted: Visual Basic Language, Opening Default Email Client Top

Thanks for the reply. I will give your solution a try, but why would your solution be considered "bad practice."

I am aware that I can send emails from within the application, however, this would require the user to set up their email settings in the application. It just seems easier to make it open up the user's default email client, which they would already have set up ready to send emails with.


 
 
ahmedilyas





PostPosted: Visual Basic Language, Opening Default Email Client Top

sure.

it was just a quick solution which works, but there are certain guidelines that should be used to make it "better", in terms of security and making sure it works almost 100% of the time. This should work.

I am a "best practice/professional" type of software developer so I maybe over stating a few things for you, just to make it confusing :-P

This solution should work (tried it) but would be even better if you P/invoked shell32.dll to get the default email client, doing it the correct way.

however, try it, see what happens!



 
 
spotty





PostPosted: Visual Basic Language, Opening Default Email Client Top

If you want to know what the executable is for the current default email client. This is stored in the following registry key - I seem to recall it as

[HKEY_CLASSES_ROOT\mailto\shell\open\command]

In my case it displays

"C:\PROGRA~1\MICROS~3\OFFICE11\OUTLOOK.EXE" -c IPM.Note /m "%1"

Which is the outlook executable with a few command line parameters.

Once you have the name of the executable which is the default mail client then you could simply start this application up using system.diagnostics.process.start


 
 
Beto Sepulveda





PostPosted: Visual Basic Language, Opening Default Email Client Top

How could I get it to input the email address displayed in the hyperlink i click on into the to box

 
 
MichP





PostPosted: Visual Basic Language, Opening Default Email Client Top

First you build a mailto string. This can have the To address, Subject, and even body text in it. This article tells you how to build it: http://support.microsoft.com/kb/192341

I use the ShellExecute API function with this string as the lpFile parameter. It looks like "ShellExecute(Handle, NULL, lpszMail, NULL, NULL, SW_SHOWNORMAL)" in C, where lpszMail is the string I built. I suppose you could also try calling the command line from HKEY_CLASSES_ROOT\mailto\shell\open\command directly, but replace the %1 with your string. The double quotes need to be around your string, but I'm not sure if the "mailto:" part of that string should be included if you do it that way.



 
 
Beto Sepulveda





PostPosted: Visual Basic Language, Opening Default Email Client Top

I forgot to mention that the hyperlinks change with each record. I built a "view application" for an employee database and when each record is viewed, it displays that person's email address. So what I am trying to accomplish is for that specific email address to populate in the "to" field. In other words, I will not be using a predefined email address.