VB.NET Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript



VB.NET Examples

Bounced Mail
Character Encoding
Digital Certificates
Digital Signatures
Email
FTP
HTML to XML
HTTP
IMAP
Encryption
MHT / HTML Email
PFX
RSA Encryption
S/MIME
Socket
Spider
Tar Archive
Upload
XML
XMP
Zip Compression
Misc

More Examples...
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor

Byte Array
VB.NET FTPS
System.IO

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

VB.NET to Decrypt S/MIME Message

Download VB.NET Project Files and Source Code

This VB.NET S/MIME example program shows how to decrypt an encrypted MIME message. Once decrypted, the message contents can be easily accessed.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Decrypt As System.Windows.Forms.Button
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Decrypt = New System.Windows.Forms.Button()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        '
        'Decrypt
        '
        Me.Decrypt.Location = New System.Drawing.Point(16, 16)
        Me.Decrypt.Name = "Decrypt"
        Me.Decrypt.Size = New System.Drawing.Size(96, 32)
        Me.Decrypt.TabIndex = 0
        Me.Decrypt.Text = "Decrypt"
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(16, 64)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(304, 199)
        Me.ListBox1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(336, 286)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.Decrypt})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' Loads an encrypted MIME message, unwraps the security envelopes, and determines
    ' if the decryption was successful.  
    Private Sub Decrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Decrypt.Click

        Dim mime As New Chilkat.Mime()

        ' Get a 30-day trial code from http:'www.chilkatsoft.com/register30.asp
        mime.UnlockComponent("UnlockCode")
        mime.LoadMimeFile("encrypted.txt")

        ' Determine if this is an encrypted message or not.
        If (mime.IsEncrypted()) Then
            ListBox1.Items.Add("Message is encrypted")
        Else
            ListBox1.Items.Add("Message is not encrypted")
        End If

        ' Unwrap the security.  
        ' The results are stored within the Mime object and can be queried
        ' afterwards. 
        ' The UnwrapSecurity does all decryption and signature validation,
        ' regardless of how complex the MIME message may be.
        Dim decryptSuccess As Boolean
        decryptSuccess = mime.UnwrapSecurity()
        If (decryptSuccess) Then
            ListBox1.Items.Add("Message has been decrypted")
        Else
            ListBox1.Items.Add("Message not decrypted")
        End If

        ' Save the decrypted MIME message.
        mime.SaveMime("decrypted.txt")
        ' (You can convert any MIME message to XML too.)
        mime.SaveXml("decrypted.xml")

        ' Get the first encrypting certificate.
        ' In complex cases where the MIME is nested and contains
        ' other encrypted MIME messages, there will be more than
        ' one encrypting certificate.
        Dim cert As Chilkat.Cert
        cert = mime.GetEncryptCert(0)
        ListBox1.Items.Add("Encrypted By: " + cert.SubjectDN)

        ' This example uses a MIME message that included a GIF attachment.
        ' Save the GIF to a file.
        Dim gifAttachment As Chilkat.Mime
        gifAttachment = mime.GetPart(1)
        gifAttachment.SaveBody("dude.gif")

    End Sub
End Class


Important: The download for this example does not contain the ChilkatDotNet.dll which
must be downloaded and installed separately at http://www.chilkatsoft.com/downloads.asp.
Once installed, add a reference to the DLL in the project by following the instructions at
http://www.example-code.com/vbdotnet/step2.asp

 
 

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

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

Mail Component · XML Parser