C#读取邮件附件的方法

基于需求需要从邮件里读取附件,从网络搜索整理如下:

1 使用 Spire.Email

从官网下载安装并引用,地址:https://www.e-iceblue.com/Download/email-for-net.html

获取附件代码如下:

using System.IO;
using Spire.Email.Pop3;  //添加命名空间

// Create a POP3 client and connect.
Pop3Client client = new Pop3Client();
client.Host = Host;
client.Username = Username;
client.Password = Password;
client.Port = 110;
client.EnableSsl = false;

client.Connect();

Spire.Email.MailMessage message = client.GetMessage(1);
//下载附件
foreach (Spire.Email.Attachment attach in message.Attachments)
{
// To get and save the attachment
string filePath = "d:\\"+ + attach.ContentType.Name;
if (File.Exists(filePath))
{
  File.Delete(filePath);
}
  //FileStream fs = File.Create(filePath);
  CopyStream(attach.Data, filePath);
}

private void CopyStream(Stream input, string filePath)
{
  FileStream fs = null;
  try
  {
fs = File.Create(filePath);
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, len);
}
  fs.Close();
}
catch (Exception)
{
if (fs != null)
{
  fs.Close();
}
  throw;
}
}

当做成服务循环执行的时候发现过段时间就报一次错,不知道是不是因为付费的原因,所以弃用寻找下一个库

2 使用 LumiSoft.Net

下载地址:http://www.lumisoft.ee/lswww/download/downloads/ 或者通过 NuGet程序包搜索下载

获取附件代码如下:

 1 using System.IO;
 2 using LumiSoft.Net;
 3 using LumiSoft.Net.Log;
 4 using LumiSoft.Net.POP3.Client;
 5 using LumiSoft.Net.MIME;
 6 using LumiSoft.Net.Mail;
 7 
 8 
 9             POP3_Client pop3 = new POP3_Client();
11             pop3.Connect(Host, Port, false);
12             pop3.Login(UserName, Pwd);//两个参数,前者为Email的账号,后者为Email的密码
15             POP3_ClientMessageCollection messages = pop3.Messages;
17             if (messages.Count <= 0)
18             {
19                 return;
20             }
21                 POP3_ClientMessage message = messages[0];//转化为POP3
22 
23                 byte[] messageBytes = message.MessageToByte();
24                 Mail_Message mime_message = Mail_Message.ParseFromByte(messageBytes);
25 
26                 MIME_Entity[] attachments = mime_message.GetAttachments(true, true);
27 
28                 foreach (MIME_Entity entity in attachments)
29                 {
30                     if (entity.ContentDisposition != null)
31                     {
32                         string fileName = entity.ContentDisposition.Param_FileName;
33                         if (!string.IsNullOrEmpty(fileName))
34                         {
35                             DirectoryInfo dir = new DirectoryInfo(@"D:\email\");
36                             if (!dir.Exists) dir.Create();
37 
38                             string path = Path.Combine(dir.FullName, fileName);
39                             MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body;
40                             Stream decodedDataStream = byteObj.GetDataStream();
41                             using (FileStream fs = new FileStream(path, FileMode.Create))
42                             {
43                                 LumiSoft.Net.Net_Utils.StreamCopy(decodedDataStream, fs, 4000);
44                             }46                         }
47                     }
48                 }
49                 message.MarkForDeletion();
52                 pop3.Disconnect();

还有其它的库没用过不做记录

记录供日后参考