dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic Classic (6 and earlier)
Expertise: Intermediate
Jun 22, 2009

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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_MAXIMIZED
End Sub

Private 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 < vbKey0 Or ASCIIcode > vbKey9) _
         And (ASCIIcode < vbKeyA Or ASCIIcode > vbKeyZ) _
         And (ASCIIcode < vbKeyA + 32 Or 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
   Next
End Function

Private Function ASCIIcode2HexChar(ASCIIcode As Integer) As String
    If ASCIIcode < 16 Then
        'need leading zero to complete 2 bytes !!!
        ASCIIcode2HexChar = "0" & Hex$(ASCIIcode)
    Else 'all ok, Hex$ returned string has 2 bytes
        ASCIIcode2HexChar = Hex$(ASCIIcode)
    End If
End Function
Loris Marcon
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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