Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

January 30, 2008

Display HTML Email in C# and VB.NET

To display HTML email (or any email) in a C# / VB.NET application, you can make use of the Microsoft HTML Object Library. In Visual Studio, select Project –> "Add Reference…", then select the "COM" tab, then scroll down and select the "Microsoft HTML Object Library". A WebBrowser control should appear in your toolbox. Drop an instance of it onto your form. The default Dock property setting is "Fill", so you may wish to create a container for it. (I used a SplitContainer.)

C# to Display HTML Email

	
    // To display the email in an embedded web browser, use CreateTempMht.
    // This saves the email to a .mht file.
    // The MHT file that is created will not contain any of the email's
    // attachments, if any existed. Also, if the email was plain-text,
    // the MHT file will be saved such that the plain-text is converted
    // to HTML using pre-formatted text ("pre" HTML tags) allowing it to
    // be displayed correctly in a WebBrowser.
    // If an empty string is passed to CreateTempMht, the filepath of a temporary
    // file is returned.  Otherwise, the input string is returned.
    string mhtFilename = email.CreateTempMht("c:/MailClientData/temp/temp.mht");
    if (mhtFilename == null)
    {
        MessageBox.Show(email.LastErrorText);
        return;
    }
	
    // Load the MHT in the WebBrowser:
    webBrowser1.Navigate("file:///C:/MailClientData/temp/temp.mht");

VB.NET to Display HTML Email

        ' To display the email in an embedded web browser, use CreateTempMht.
        ' This saves the email to a .mht file.
        ' The MHT file that is created will not contain any of the email's
        ' attachments, if any existed. Also, if the email was plain-text,
        ' the MHT file will be saved such that the plain-text is converted
        ' to HTML using pre-formatted text ("pre" HTML tags) allowing it to
        ' be displayed correctly in a WebBrowser.
        ' If an empty string is passed to CreateTempMht, the filepath of a temporary
        ' file is returned.  Otherwise, the input string is returned.
        Dim mhtFilename As String
        mhtFilename = email.CreateTempMht("c:/MailClientData/temp/temp.mht")
        If (mhtFilename = Nothing) Then
            MessageBox.Show(email.LastErrorText)
            Exit Sub
        End If
	
        ' Load the MHT in the WebBrowser:
        WebBrowser1.Navigate("file:///C:/MailClientData/temp/temp.mht")


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.