| Author |  | 
      
        | adam.kozovo Newbie
 
  
 
 Joined: 30 June 2023
 Online Status: Offline
 Posts: 19
 | 
          Hello. do you like to make SmimeResult and MsgConvert Classes disposable to dispose message and certificates from SmimeResult afterwards when used with USING? Such classes which contains sensitive info such as certificates shall be disposable, am I correct?
           | Posted: 12 July 2023 at 9:56pm | IP Logged |   |  
           | 
 |  
 
 And after loading an email to EmailMessage , may I always use this code for all kind of messages? (both encrypted and non-encrypted messages)
 
 
 Dim SmimeResult1 As SmimeResult = Smime1.DecryptAndVerify(EmailMessage1, MessageVerificationFlags.All, Nothing, Nothing)
 
 If SmimeResult1 IsNot Nothing AndAlso SmimeResult1.DecryptedMessage IsNot Nothing Then EmailMessage = SmimeResult1.DecryptedMessage
 
 SmimeResult1.DecryptedMessage.Dispose()
 SmimeResult1 = Nothing
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | Alex AfterLogic Support
 
  
  
 Joined: 19 November 2003
 Online Status: Offline
 Posts: 2207
 | 
          Hello,
           | Posted: 13 July 2023 at 2:42am | IP Logged |   |  
           | 
 |  
 MsgConvert does not deal with unmanaged resources.
 
 Neither SmimeResult does. Although you can see Dispose in Certificate class the truth is that it remains for backward compatibility only. It does nothing. The original version of MailBee.NET Objects relied on Win32 implementation of certificates and thus indeed used unmanaged resource. However, since .NET 2.0 which added .NET's native support of certificates, this has been rewritten to re-use the managed APIs only.
 
 Regards,
 Alex
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | adam.kozovo Newbie
 
  
 
 Joined: 30 June 2023
 Online Status: Offline
 Posts: 19
 | 
          Hello
           | Posted: 14 July 2023 at 11:10pm | IP Logged |   |  
           | 
 |  Thanks for your detailed response, you're right as Dispose was added since .net framework 4.6
 https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate.dispose?view=netframework-4.6
 
 Still makes sense to have a disposable class and manually clear certificate collections and set certificates to nothing:
 https://snede.net/the-most-dangerous-constructor-in-net/
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | adam.kozovo Newbie
 
  
 
 Joined: 30 June 2023
 Online Status: Offline
 Posts: 19
 | 
          Sorry forgot to add: after loading an email to EmailMessage , may I always use this code for all kind of messages? (both encrypted and non-encrypted messages)
           | Posted: 14 July 2023 at 11:15pm | IP Logged |   |  
           | 
 |  If I just need the decrypted messages without any certificate info:
 
 
 Dim SmimeResult1 As SmimeResult = Smime1.DecryptAndVerify(EmailMessage1, MessageVerificationFlags.All, Nothing, Nothing)
 
 If SmimeResult1 IsNot Nothing AndAlso SmimeResult1.DecryptedMessage IsNot Nothing Then EmailMessage = SmimeResult1.DecryptedMessage
 
 SmimeResult1.DecryptedMessage.Dispose()
 SmimeResult1 = Nothing
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | Alex AfterLogic Support
 
  
  
 Joined: 19 November 2003
 Online Status: Offline
 Posts: 2207
 | 
          Hi,
           | Posted: 18 July 2023 at 3:51am | IP Logged |   |  
           | 
 |  
 You're confusing X509Certificate with X509Certificate2. MailBee.NET does not deal with X509Certificate class, only with X509Certificate2 which does not support (neither does it need) Dispose.
 
 As for DecryptAndVerify, mostly yes but I'd recommend DecryptAndVerify2 instead of DecryptAndVerify as it's .NET Standard 2.0 compliant and does not rely on Win32 API.
 
 Regards,
 Alex
 | 
       
        | Back to Top |     | 
       
       
        |  |