Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Open the default program for sending email messages

Total Hit ( 2542)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


The ShellExecute API function recognizes email addresses if they are prefixed by the "mailto:" prefix, and correctly run the default program for sending email messages (e.g. Outlook). This lets you open a window for sending an email and automatically fill the address field. Here's a wrapper routine that encapsulates the low-level details:

Click here to copy the following block
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
  (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
  ByVal lpParameters As String, ByVal lpDirectory As String, _
  ByVal nShowCmd As Long) As Long

' Open the default program for sending email messages
' Returns True if successful, False otherwise

Public Function OpenEmailProgram(ByVal EmailAddress As String) As Boolean
  Dim res As Long
  res = ShellExecute(0&, "open", "mailto:" & EmailAddress, vbNullString, _
    vbNullString, vbNormalFocus)
  OpenEmailProgram = (res > 32)
End Function

You should always check the return value of the function, in case no program has been registered on the machine:
If Not OpenEmailProgram("username@domain.com") Then
  MsgBox "Unable to run the email program"
End If

UPDATE: It turns out that you can even provide the contents for other mail fields, by passing additional values on the URL. The following updated version of the routine above supports a subject, the body of the message, a CC mail address and a BCC address. All these added arguments are optional:

Click here to copy the following block
Sub OpenEmailProgram(sDest As String, Optional sSubject As String, _
  Optional sBody As String, Optional sCC As String, Optional sBCC As String)
  ShellExecute 0, vbNullString, "mailto:" & sDest & "?subject=" & sSubject & _
    "&body=" & sBody & "&CC=" & sCC & "&BCC=" & sBCC, 0&, 0&, 1
End Sub

' how to use:
SendMail "test1@vb2themax.com", "SendMail Test", "The function works!!!", _
  "test2@hotmail.com, test3@hotmail.com"

UPDATE: This approach has one limitation under Windows NT4, whose Shell function can pass parameters longer than 260 characters and can therefore truncate long mail addresses. Unfortunately there isn't any simple workaround (this problem has been solved in Windows 2000, though).

For more information see: MSKB Article Q269272.



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.