This project is read-only.

Error while decrypting a message with SmimeDevelopeAndDecrypt

Dec 26, 2012 at 11:00 AM

Message with an attachment is encrypted using method ActiveUp.Net.Mail.Message.SmimeEnvelopeAndEncryptFor, sent to mailbox using method ActiveUp.Net.Mail.SmtpClient.Send.

Message is received using method ActiveUp.Net.Mail.Pop3Client.RetrieveMessageObject

Error while decrypting a message using method ActiveUp.Net.Mail.Message.SmimeDevelopeAndDecrypt : Exception calling "SmimeDevelopeAndDecrypt" with "1" argument(s): "This message doesn't seem to be encrypted, or the encryption method is unknown."

Source code create message and encrypt in PowerShell:

    try
    {
        $message = New-Object ActiveUp.Net.Mail.Message
        
        $message.From.Email = $SenderEmail
        
        $message.Subject = $Subject
        
        if ($IsBodyHtml) { $message.BodyHtml.Text = $Body }
        else { $message.BodyText.Text = $Body }
        
        for ($i=0;$i -lt $RecipientsEmail.Count;$i++) { $message.To.Add($RecipientsEmail[$i]) }
        
        if (($Attachments -ne $null) -and ($Attachments.Count -gt 0))
        {
            for ($i=0;$i -lt $Attachments.Count;$i++) { $message.Attachments.Add($Attachments[$i], $true) }
        } # if (($Attachments -ne $null) -and ($Attachments.Count -gt 0))
        
        # It is required to build the mime part tree before encrypting
        $message.BuildMimePartTree()
        
        $recipients = New-Object System.Security.Cryptography.Pkcs.CmsRecipientCollection

        for ($i=0;$i -lt $RecipientsCertificate.Count;$i++) 
        { 
            $RecipientCertificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($RecipientsCertificate[$i])
            $recipient = New-Object System.Security.Cryptography.Pkcs.CmsRecipient($RecipientCertificate)
            $recipients.Add($recipient)
        }

       
        $message.SmimeEnvelopeAndEncryptFor($recipients)
        
        [ActiveUp.Net.Mail.SmtpClient]::Send($message, $SmtpServer)
        
        $retflag = $true
    } # try
    catch
    {
        write_host $_.Exception.Message
        
        if ($SmtpClient.Connected) { $SmtpClient.Disconnect() }
    } # catch

Source code received message and decrypt in PowerShell:

    try
    {
            $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($cert_pfx, $cert_pasw)
            $certcol = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection($cert)

$pop = New-Object ActiveUp.Net.Mail.Pop3Client
$pop.Connect($host, $user, $pasw)

for ($i = 1 ; $i -le $pop.MessageCount; $i++)
{
    $msg = $pop.RetrieveMessageObject($i)

    $originalMessage = $msg.SmimeDevelopeAndDecrypt($certcol)

    if ($originalMessage.LeafMimeParts.Count -gt 0)
    {
        $l = 0
        for ($k = 0; $k -lt $originalMessage.LeafMimeParts.Count; $k++)
        {
            if ($originalMessage.LeafMimeParts[$k].Filename.Length -gt 0)
            {
                $l++
                $fname = $('D:\Temp\$$\' + $msg.MessageId + '_' + $l + '_' + $originalMessage.LeafMimeParts[$k].Filename)
                Write-Host $fname
                $originalMessage.LeafMimeParts[$k].StoreToFile($fname)
            }
        }
    }
}

if ($pop.Connected) { $pop.Disconnect() }

    } # try
    catch
    {
       write-host $_.Exception.Message
        if ($pop.Connected) { $pop.Disconnect() }
        }
    } # catch

Apr 4, 2014 at 7:31 AM
Hi

I know this is an old thread. Did you ever get a solution?
I have the same issue.
I am signing and encrypting a message.
When checking the IsSmimeEncrypted, the result is always false.
Is there another step to do this here?
// It is required to build the mime part tree before encrypting
message.BuildMimePartTree();

var signer = new CmsSigner(clientCertificate);

// Here we only want the signer's certificate to be sent along. Not the whole chain.
signer.IncludeOption = X509IncludeOption.EndCertOnly;

message.SmimeAttachSignatureBy(signer);
message.SmimeEnvelopeAndEncryptFor(recipients);

bool IsSmimeEncrypted = message.IsSmimeEncrypted;
Apr 4, 2014 at 8:09 AM
Hi

Unfortunately, I'm so nobody answered.
I myself have not been able to find a solution.
So I refused to use this library.
May 31, 2014 at 1:05 AM
If you want working S/MIME support, I recommend using MailKit which not only works on Windows, but also Mono (which means it works on Linux, MacOS, iOS, and Android among other platforms).