繼上篇反射方案,發現 `SmtpClient` 內建 `SpecifiedPickupDirectory` 模式可直接生成 EML 文件,無需反射,極度簡單。
`SmtpClient.DeliveryMethod` 為 `SpecifiedPickupDirectory` 時,`Send` 會在指定目錄生成 EML 文件,完全無網絡交互。IL 反編譯證實內部調用 `MailWriter` 寫入流,流向同反射方案。
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = false; // 必須關閉
smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtp.PickupDirectoryLocation = @"d:\mail\";
MailMessage msg = new MailMessage();
msg.Subject = "Hello";
msg.From = new MailAddress("me@example.com");
msg.To.Add("you@example.com");
msg.Body = "Body content";
smtp.Send(msg); // 直接在 d:\mail\ 生成 {GUID}.eml