emlFileReader.cs 1.5 KB

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