Visual Basic Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

VB Examples

Bounced Mail
Bz2
Character Encoding
CSV
Digital Certificates
Digital Signatures
Email
FTP
HTML-to-XML
HTTP
IMAP
Encryption
MHT / HTML Email
POP3
RSA
S/MIME
SFTP
SMTP
Socket
Spider
SSH
SSH Key
SSH Tunnel
String
Tar
Unicode
Upload
XML
XMP
Zip Compression

More Examples...
Email Object
FileAccess
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA


VB Strings
VB Byte Array

Unreleased...
Bzip2
LZW
Icon

 

 

 

 

 

 

 

Display HTML Email in a Web Browser Control

Download Chilkat Email ActiveX for POP3 / SMTP

This VB6 example demonstrates how to display HTML email in a WebBrowser control. Your VB application can display any HTML mail as it would appear in Internet Explorer.

' This example shows how to display any email in a WebBrowser control.
' The key to doing this is in the new CreateTempMht method.
' Look for the method call in the code below, and you will find more explanation
' for how it works.

Dim TempMhtFilename As String
Dim CurrentMailIdx As Long
Dim MailBundle As New ChilkatEmailBundle2

' Initialize our Web Browser control to "about:blank"
Private Sub Form_Load()
    TempMhtFilename = ""
    WebBrowser1.Navigate2 ("about:blank")

End Sub

' Resize the browser with our app.
Private Sub Form_Resize()
    WebBrowser1.Width = Form1.Width - 340
    WebBrowser1.Height = Form1.Height - 2640
End Sub


Private Sub UpdateBrowser()
    If MailBundle.MessageCount = 0 Then
        WebBrowser1.Navigate2 ("about:blank")
        Exit Sub
    End If
    
    If (CurrentMailIdx >= MailBundle.MessageCount) Then
        CurrentMailIdx = MailBundle.MessageCount - 1
    End If
    
    Dim email As ChilkatEmail2
    Set email = MailBundle.GetEmail(CurrentMailIdx)
    
    OnlyRefresh = True
    If (TempMhtFilename = "") Then
        OnlyRefresh = False
    End If
    
    ' The CreateTempMht method does not display the HTML file directly,
    ' but rather creates a file such that you can call the WebBrowser.Navigate2 method
    ' to display the email.
    ' CreateTempMht creates a temporary .mht file such that:
    ' 1) If attachments are present in the email, the .mht file will not contain them.
    ' 2) If the email is not already HTML (i.e. it is plain text) the .mht will contain an HTML representation of the email
    '    such that the plain-text is displayed as pre-formatted HTML using the <pre> ... </pre> HTML tags.
    ' 3) If TempMhtFilename is empty, then CreateTempMht will automatically generate a temporary filename.
    '    If TempMhtFilename is not empty, CreateTempMht uses that filename, and return value is simply the
    '    same string.
    '    This allows you to easily create a temp file and the re-use it.  When your app exits, you would delete the file, but
    '    that is left for you to do.
    '
    ' You can see that this example will Navigate the WebBrowser to the new temp file after it is
    ' first created, but afterwards it will simply Refresh when the temp file is rewritten.
    TempMhtFilename = email.CreateTempMht(TempMhtFilename)
    
    If (OnlyRefresh) Then
        WebBrowser1.Refresh2
    Else
        WebBrowser1.Navigate2 TempMhtFilename, navNoReadFromCache
    End If
    
    Set email = Nothing
    
End Sub

' Retrieve mail from the POP3 server and store it in
' an email bundle object.
Private Sub GetEmail_Click()
    Dim mailman As ChilkatMailMan2
    Set mailman = New ChilkatMailMan2
    mailman.UnlockComponent UnlockCode.Text
    mailman.MailHost = Pop3Host.Text
    mailman.PopUsername = Pop3Login.Text
    mailman.PopPassword = Pop3Password.Text
    
    Set MailBundle = mailman.CopyMail()
    
    CurrentMailIdx = 0
    
    UpdateBrowser
    
    Status.Caption = "Copied " & Str(MailBundle.MessageCount) & " messages from the server"
    
End Sub

' Display the next email in the bundle.
Private Sub NextEmail_Click()
    CurrentMailIdx = CurrentMailIdx + 1
    UpdateBrowser
    
    If MailBundle.MessageCount = 0 Then
        Status.Caption = "There are no emails to display"
    Else
        Status.Caption = "Showing message " & Str(CurrentMailIdx + 1) & " out of " & Str(MailBundle.MessageCount)
    End If
            
End Sub

' Display the previous email in the bundle.
Private Sub PreviousEmail_Click()
    If (CurrentMailIdx > 0) Then
        CurrentMailIdx = CurrentMailIdx - 1
        UpdateBrowser
        
        If MailBundle.MessageCount = 0 Then
            Status.Caption = "There are no emails to display"
        Else
            Status.Caption = "Showing message " & Str(CurrentMailIdx + 1) & " out of " & Str(MailBundle.MessageCount)
        End If
    End If

End Sub


 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.