Following code will create message, add attachment, encrypt the whole
message and send it. Certificate is loaded from the file, but it could
be loaded from elsewhere (e.g. from Windows certicate store).
using Rebex.Mail;
using Rebex.Mime.Headers;
using Rebex.Security.Certificates;
...
// load the certificate
Certificate recipient = Certificate.LoadDer("joe.cer");
Certificate sender = Certificate.LoadDer("hugo.cer");
// create an instance of MailMessage
MailMessage message = new MailMessage();
// set its properties to desired values
message.From = "hugo@example.com";
message.To = "joe@example.com";
message.Subject = "This is a simple message";
message.BodyText = "Hello, Joe!";
message.BodyHtml = "Hello, <b>Joe</b>!";
message.Attachments.Add
(
new Attachment("c:\\data.zip")
);
// Encrypt it using Joe's certificate.
// When using a message.Encrypt(recipient) call, only the reciepient
// will be able to decrypt the message. Following code will
// allow the sender to decrypt it later as well.
message.Encrypt(recipient, sender);
Smtp.Send(message, "smtp.server.com");
Further reading