devxlogo

Fast Way to Create and Send Email

Fast Way to Create and Send Email

An easy way to create and send email in VB (or any other language) is to use the “mailto:” protocol that already works on client computers. The technique makes a request to the installed email client, and works with Outlook, Lotus Notes, and other registered email clients.

You can’t use the standard VB Shell function; instead, you call the Windows ShellExecute API to send email.

Put this code in a module called Module1.bas:

Public Declare Function ShellExecute Lib "shell32" _   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   'Constants for ShowWindow (nCmdShow)   Public Const SW_HIDDEN As Long = 0   Public Const SW_NORMAL As Long = 1   Public Const SW_MINIMIZED As Long = 2   Public Const SW_MAXIMIZED As Long = 3   Public Const SW_NOTACTIVE As Long = 4   Public Const SW_UNHIDDEN As Long = 5   Public Const SW_MINWITHFOCUS As Long = 6   Public Const SW_MINNOTACTIVE As Long = 7   Public Const SW_RESTORE As Long = 9

You’ll need a form to enter an email address. Open the default Form1.frm, in design mode, and place three Textboxes and a Command Button on the form. Then add this code to handle the control events:

Private Sub Command1_Click()   Dim Email As String   Email = "mailto:" & Text1.Text _      & "?subject=" & Text2.Text _      & "&body=" & MailTo_StringConverter(Text3.Text)   ShellExecute Me.hwnd, vbNullString, _      Email, vbNullString, vbNullString, SW_MAXIMIZEDEnd SubPrivate Function MailTo_StringConverter(Text As String) As String   Dim CharIndex As Integer   Dim CharLength As Integer   Dim ASCIIcode As Integer       MailTo_StringConverter = Text   'easy tecnique to speed up For cicle!!   CharLength = Len(MailTo_StringConverter)       For CharIndex = CharLength To 1 Step -1      ASCIIcode = Asc(Mid$(MailTo_StringConverter, CharIndex, 1))      If (ASCIIcode  vbKey9) _         And (ASCIIcode  vbKeyZ) _         And (ASCIIcode  vbKeyZ + 32) Then         ' replace punctuation chars with "%hex"         MailTo_StringConverter = Left$( _            MailTo_StringConverter, CharIndex - 1) _            & "%" & ASCIIcode2HexChar(ASCIIcode) _            & Mid$(MailTo_StringConverter, CharIndex + 1)      End If   NextEnd FunctionPrivate Function ASCIIcode2HexChar(ASCIIcode As Integer) As String    If ASCIIcode 

devx-admin

Share the Post: