emlFileReader.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using MimeKit;
  2. using System;
  3. using System.IO;
  4. namespace wispro.sp.winClient
  5. {
  6. public class emlFileReader
  7. {
  8. private string _filePath;
  9. public emlFileReader(string filePath) {
  10. _filePath = filePath;
  11. using (var stream = File.OpenRead(_filePath))
  12. {
  13. var message = MimeMessage.Load(stream);
  14. // 遍历所有附件
  15. foreach (var attachment in message.Attachments)
  16. {
  17. if (attachment is MimePart part && part.IsAttachment)
  18. {
  19. string fileName = part.FileName;
  20. string contentType = part.ContentType.ToString();
  21. // 保存附件到文件系统
  22. string savePath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
  23. using (var output = File.Create(savePath))
  24. {
  25. part.Content.DecodeTo(output);
  26. }
  27. Console.WriteLine($"附件 '{fileName}' 已保存,类型: {contentType}");
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }