using System.Text; namespace Khaemhat.Infrastructure.Tests.Mail; /// /// Certified mail messages built in the test, shaped like the ones a PEC provider delivers: a multipart/mixed /// envelope with the text of the provider, daticert.xml, the original message as postacert.eml /// (message/rfc822) and the signature smime.p7s. Nothing is downloaded. /// internal static class PecSamples { /// A certified message whose original was sent by with the given files. public static byte[] Envelope(string sender, string subject, params (string Name, byte[] Content)[] files) { var inner = new StringBuilder() .Append("From: ").Append(sender).Append("\r\n") .Append("To: fatture@pec.example.it\r\n") .Append("Subject: ").Append(subject).Append("\r\n") .Append("MIME-Version: 1.0\r\n") .Append("Content-Type: multipart/mixed; boundary=\"inner-b\"\r\n\r\n") .Append("--inner-b\r\n") .Append("Content-Type: text/plain; charset=utf-8\r\n\r\n") .Append("Messaggio del Sistema di Interscambio\r\n"); foreach (var (name, content) in files) { inner.Append("--inner-b\r\n") .Append("Content-Type: application/octet-stream; name=\"").Append(name).Append("\"\r\n") .Append("Content-Transfer-Encoding: base64\r\n") .Append("Content-Disposition: attachment; filename=\"").Append(name).Append("\"\r\n\r\n") .Append(Wrap(Convert.ToBase64String(content))).Append("\r\n"); } inner.Append("--inner-b--\r\n"); var outer = new StringBuilder() .Append("From: \"Per conto di: ").Append(sender).Append("\" \r\n") .Append("To: fatture@pec.example.it\r\n") .Append("Subject: =?utf-8?B?").Append(Convert.ToBase64String(Encoding.UTF8.GetBytes("POSTA CERTIFICATA: " + subject))).Append("?=\r\n") .Append("X-Trasporto: posta-certificata\r\n") .Append("MIME-Version: 1.0\r\n") .Append("Content-Type: multipart/signed; protocol=\"application/pkcs7-signature\"; micalg=sha-256;\r\n boundary=\"signed-b\"\r\n\r\n") .Append("--signed-b\r\n") .Append("Content-Type: multipart/mixed; boundary=\"outer-b\"\r\n\r\n") .Append("--outer-b\r\n") .Append("Content-Type: text/plain; charset=iso-8859-1\r\n") .Append("Content-Transfer-Encoding: quoted-printable\r\n\r\n") .Append("Messaggio di posta certificata: il messaggio originale =E8 allegato.\r\n") .Append("--outer-b\r\n") .Append("Content-Type: application/xml; name=daticert.xml\r\n") .Append("Content-Disposition: inline; filename=daticert.xml\r\n\r\n") .Append("\r\n") .Append("--outer-b\r\n") .Append("Content-Type: message/rfc822; name=postacert.eml\r\n") .Append("Content-Disposition: inline; filename=postacert.eml\r\n\r\n") .Append(inner) .Append("--outer-b--\r\n") .Append("--signed-b\r\n") .Append("Content-Type: application/pkcs7-signature; name=smime.p7s\r\n") .Append("Content-Transfer-Encoding: base64\r\n") .Append("Content-Disposition: attachment; filename=smime.p7s\r\n\r\n") .Append(Convert.ToBase64String([1, 2, 3, 4])).Append("\r\n") .Append("--signed-b--\r\n"); return Encoding.UTF8.GetBytes(outer.ToString()); } /// The acceptance receipt of the provider: no original message, no files of the SDI. public static byte[] Acceptance(string subject) => Encoding.UTF8.GetBytes( "From: posta-certificata@pec.provider.example\r\n" + "Subject: ACCETTAZIONE: " + subject + "\r\n" + "X-Ricevuta: accettazione\r\n" + "Content-Type: multipart/mixed; boundary=\"a\"\r\n\r\n" + "--a\r\nContent-Type: text/plain\r\n\r\nIl messaggio e' stato accettato dal sistema.\r\n" + "--a\r\nContent-Type: application/xml; name=daticert.xml\r\nContent-Disposition: attachment; filename=daticert.xml\r\n\r\n\r\n" + "--a--\r\n"); private static string Wrap(string base64) { var lines = new StringBuilder(); for (var i = 0; i < base64.Length; i += 76) { lines.Append(base64, i, Math.Min(76, base64.Length - i)).Append("\r\n"); } return lines.ToString().TrimEnd(); } }