How to Display
HTML or Plain-Text Email in a WebBrowser Control using Chilkat
Mail
Chilkat
Mail v4.4.0 provides a new method to assist in displaying HTML
or plain-text mail in a WebBrowser control. The new method, CreateTempMht,
creates an MHT Web Archive (.mht file) allowing you to navigate
the WebBrowser to it and display the contents. The CreateTempMht
method has these features:
- Attachments
are not saved to the MHT file. Because they are not part of
the display, attachments are not included in the .MHT to allow
the WebBrowser to process and display the file faster.
- If
the email is plain-text, the MHT file will contain an HTML version
of it such that the plain-text is converted to HTML and encapsulated
as pre-formatted text.
- You
can either let CreateTempMht generate a temporary filename,
or you can specify the filename to be created or overwritten.
A
fully-functional Visual Basic example program is provided in the
Chilkat Mail download.
Here is the source code of that example:
'
' This example shows how to display any email in a WebBrowser control.
' The key to doing this is in the new CreateTempMht method that is provided
' in Chilkat Mail and Chilkat Email.NET version 4.4.0
' 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 ChilkatEmailBundle
Private Sub Form_Load()
TempMhtFilename = ""
WebBrowser1.Navigate2 ("about:blank")
End Sub
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 ChilkatEmail
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
Private Sub GetEmail_Click()
Dim mailman As ChilkatMailMan
Set mailman = New ChilkatMailMan
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
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
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
|
|
Copyright
2000-2005 Chilkat Software, Inc. All rights reserved.
Components for Microsoft Windows XP, 2000, NT, 95/98/ME
|