1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| /// <summary>
/// Chiffre et signe le contenu d'un mail
/// </summary>
/// <param name="content">Contenu à signer et chiffrer</param>
/// <param name="signatureCertificate">Certificat à utiliser pour la signature du contenu</param>
/// <param name="encryptionCertificate">Certificat à utiliser pour le chiffrage du contenu</param>
/// <returns></returns>
private static string getSecuredContent(string content, string subject, X509Certificate2 signatureCertificate, X509Certificate2 encryptionCertificate)
{
ContentInfo originalInfo = new ContentInfo(Encoding.UTF8.GetBytes(content));
CmsSigner signer = new CmsSigner(signatureCertificate);
SignedCms signedEnv = new SignedCms(originalInfo);
EnvelopedCms cryptedEnv = new EnvelopedCms();
CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, encryptionCertificate);
StringBuilder signedContent = new StringBuilder();
StringBuilder cryptedContent = new StringBuilder();
signer.SignedAttributes.Add(new Pkcs9SigningTime());
signedEnv.ComputeSignature(signer);
signedContent.AppendLine("Content-Type: application/pkcs7-mime; name=smime.p7m; smime-type=signed-data");
signedContent.AppendLine("Content-Transfer-Encoding: base64");
signedContent.AppendLine("Content-Disposition: attachment; filename=\"smime.p7m\"");
signedContent.AppendLine("Content-Description: S/MIME Cryptographic Signed Data");
signedContent.AppendLine();
signedContent.AppendLine(Convert.ToBase64String(signedEnv.Encode(), Base64FormattingOptions.InsertLineBreaks));
cryptedEnv = new EnvelopedCms(new ContentInfo(Encoding.ASCII.GetBytes(signedContent.ToString())));
cryptedEnv.Encrypt(recipient);
cryptedContent.AppendLine("Content-Type: application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data");
cryptedContent.AppendLine("Content-Transfer-Encoding: base64");
cryptedContent.AppendLine("Content-Disposition: attachment; filename=\"smime.p7m\"");
cryptedContent.AppendLine("Content-Description: S/MIME Encrypted Message");
cryptedContent.AppendLine("From: =?utf-8?Q?Expediteur");
cryptedContent.AppendLine("To: rcp.cancerologie@esante-poitou-charentes.fr");
cryptedContent.AppendLine("Subject: " + subject);
cryptedContent.AppendLine("MIME-Version: 1.0");
cryptedContent.AppendLine("Date: " + DateTime.Now.ToString("R"));
cryptedContent.AppendLine();
cryptedContent.AppendLine(Convert.ToBase64String(cryptedEnv.Encode(), Base64FormattingOptions.InsertLineBreaks));
cryptedContent.AppendLine(".");
return cryptedContent.ToString();
}
/// <summary>
/// Envoi un contenu sécurisé
/// </summary>
/// <param name="mailContent">Contenu signé et chiffré à envoyer</param>
/// <param name="toEmail">Adresse e-mail du destinataire</param>
/// <returns>True si le mail a été envoyé, false sinon</returns>
internal static bool sendSecuredMail(string mailContent, string toEmail)
{
try
{
using (HigLabo.Net.Smtp.SmtpClient smtpClient = new HigLabo.Net.Smtp.SmtpClient(AppInfo.SmtpServer))
{
SmtpMessage mail = new SmtpMessage();
smtpClient.HostName = AppInfo.SmtpServer;
smtpClient.Port = AppInfo.SmtpPort;
//smtpClient.Ssl = true;
smtpClient.EncryptedCommunication = SmtpEncryptedCommunication.Ssl;
smtpClient.AuthenticateMode = SmtpAuthenticateMode.Login;
smtpClient.UserName = AppInfo.SmtpUser;
smtpClient.Password = AppInfo.SmtpPassword;
SendMailResult rs = smtpClient.SendMail(AppInfo.SmtpSender, toEmail, String.Empty, String.Empty, System.Text.Encoding.ASCII.GetBytes(mailContent));
Trace.WriteLine(rs.SendSuccessful ? "Message sécurisé envoyé" : "Erreur lors de l'envoi du message sécurisé");
Trace.WriteLine(rs.Message);
return rs.SendSuccessful;
}
}
catch (Exception e)
{
Trace.WriteLine("=== EXCEPTION ===");
Trace.WriteLine(e.ToString());
Trace.WriteLine(e.Message);
Trace.WriteLine(e.StackTrace);
return false;
}
} |
Partager