MailBee.NET Objects 4.0

Smime.Sign Method 

Signs an e-mail message with a digital signature.

public MailMessage Sign(
   MailMessage message,
   Certificate signingCert
);

Parameters

message
The original e-mail message to be signed.
signingCert
The certificate to be used for signing the message. This certificate must contain a private key.

Return Value

A reference to the signed message if the signing went successfully; a reference to the original message if it was already signed; a null reference (Nothing in Visual Basic) if the signing process failed.

Remarks

Make sure the Certificate used for signing has the same EmailAddress as Email value of MailMessage.From of the message being signed. Otherwise, the message will still be signed successfully but it might not be trusted by its recipients when it gets delivered.

To create a signature, MailBee uses HashAlgorithm and the private key of signingCert to calculate a hash of the message data.

The developer can also use the SignAndEncrypt method to sign and encrypt a message within a single method call.

Exceptions

Exception TypeCondition
MailBeeInvalidArgumentExceptionmessage or signingCert is a null reference (Nothing in Visual Basic).
MailBeeSmimeWin32ExceptionAn error occurred and ThrowExceptions is true.

Example

The following sample loads the message from disk, signs it with the sender's certificate, and saves it back to disk.

[C#]
// To use the code below, import these namespace at the top of your code
using System;
using MailBee;
using MailBee.Mime;
using MailBee.Security;

// The actual code (put it into a method of your class)

// Load the message from file.
MailMessage msg = new MailMessage();
msg.LoadMessage(@"C:\Docs\original.eml");

Smime objSmime = new Smime();

try
{
    // Load certificate from the specified file.
    Certificate signingCert = new Certificate(@"C:\Docs\cert.pfx", CertFileType.Pfx, "secret");
    // Sign the message.
    MailMessage signMsg = objSmime.Sign(msg, signingCert);
    // Save the signed message to disk.
    signMsg.SaveMessage(@"C:\Docs\signed.eml");
    Console.WriteLine("Done.");
}
catch (MailBeeException ex)
{
    Console.WriteLine(ex.Message);
}
[Visual Basic]
' To use the code below, import MailBee namespace at the top of your code
Imports MailBee
Imports MailBee.Mime
Imports MailBee.Security

' The actual code (put it into a method of your class)

' Load the message from file.
Dim msg As MailMessage = New MailMessage
msg.LoadMessage("C:\Docs\original.eml")

Dim objSmime As Smime = New Smime

Try
    ' Load certificate from the specified file.
    Dim signingCert As Certificate = New Certificate("C:\Docs\cert.pfx", CertFileType.Pfx, "secret")
    ' Sign the message.
    Dim signMsg As MailMessage = objSmime.Sign(msg, signingCert)
    ' Save the signed message to disk.
    signMsg.SaveMessage("C:\Docs\signed.eml")
    Console.WriteLine("Done.")
Catch ex As MailBeeException
    Console.WriteLine(ex.Message)
End Try

See Also

Smime Class | MailBee.Security Namespace | SignAndEncrypt