Files
Khaemhat/tests/Khaemhat.Infrastructure.Tests/Mail/PecSamples.cs
T
Alby96andClaude Opus 5.5 709bb13532 feat: FatturaPA writer, validator, SDI receipts and transport channels (#74 #75 #76 #77)
The Italian pack can now produce and check the e-invoice and talk to the SDI:
- FatturaPaWriter writes FPR12/FPA12 v1.2 with SDI file naming; FatturaPaValidator
  checks it against an embedded schema and the AdE semantic rules (official codes).
- SdiReceiptReader reads RC, NS, MC, NE, DT, AT, MT and SE notifications.
- The incoming-invoice model becomes the shared EInvoice* model, read and written.
- In-house ImapClient and MimeParser (no external libraries) back the PEC channel;
  SdICoop (SOAP/MTOM with client certificate) and two generic REST intermediaries
  sit behind the same IEInvoiceTransport contract, with fake-server tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 11:51:31 +01:00

87 lines
4.6 KiB
C#

using System.Text;
namespace Khaemhat.Infrastructure.Tests.Mail;
/// <summary>
/// Certified mail messages built in the test, shaped like the ones a PEC provider delivers: a <c>multipart/mixed</c>
/// envelope with the text of the provider, <c>daticert.xml</c>, the original message as <c>postacert.eml</c>
/// (<c>message/rfc822</c>) and the signature <c>smime.p7s</c>. Nothing is downloaded.
/// </summary>
internal static class PecSamples
{
/// <summary>A certified message whose original was sent by <paramref name="sender"/> with the given files.</summary>
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("\" <posta-certificata@pec.provider.example>\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("<postacert tipo=\"posta-certificata\"/>\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());
}
/// <summary>The acceptance receipt of the provider: no original message, no files of the SDI.</summary>
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<postacert tipo=\"accettazione\"/>\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();
}
}