C# 使用POP3如何下载excel附件
下面的代码适用于CSV文件,但当我们使用相同的代码读取excel文件时,它会在文件中返回不同的字母C# 使用POP3如何下载excel附件,c#,C#,下面的代码适用于CSV文件,但当我们使用相同的代码读取excel文件时,它会在文件中返回不同的字母 foreach (MimeAttachment attach in msg.Attachments) { if (attach.Filename != "") { //read data from attachment string myString = attach.Body.Replace("=\r\n", ""); //to eliminate the '=\r\n' from email
foreach (MimeAttachment attach in msg.Attachments)
{
if (attach.Filename != "")
{
//read data from attachment
string myString = attach.Body.Replace("=\r\n", ""); //to eliminate the '=\r\n' from email attachemnt
Byte[] b = GetBytes(myString);
// save attachment to disk
System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false);
FileStream outStream = new FileStream(DownloadFilePath + "\\" + attach.Filename, FileMode.Create);
mem.WriteTo(outStream);
mem.Close();
outStream.Flush();
outStream.Close();
}
}
例:
这对我有用
foreach (OpenPop.Mime.MessagePart attachment in attachments)
{
if (attachment != null)
{
string ext = attachment.FileName.Split('.')[1];
Filename = DateTime.Now.Ticks.ToString();
FileInfo file = new FileInfo((AppDomain.CurrentDomain.BaseDirectory + "Downloaded//") + Filename + ".csv" );
// Check if the file already exists
if (!file.Exists)
{
attachment.Save(file);
}
}
}
或者试试这个
您是否尝试了Excel附件的代码,我的代码也适用于CSV问题所在Excel@Ashaar你试过这个吗?您是否使用任何dll获取邮件?是的,我正在使用'smtpop.dll'在您的情况下,您正在使用的dll是什么?它是开源的还是需要购买谢谢。是的,通过使用OpenPop,我们可以按原样执行文件下载。
foreach (OpenPop.Mime.MessagePart attachment in attachments)
{
if (attachment != null)
{
string ext = attachment.FileName.Split('.')[1];
Filename = DateTime.Now.Ticks.ToString();
FileInfo file = new FileInfo((AppDomain.CurrentDomain.BaseDirectory + "Downloaded//") + Filename + ".csv" );
// Check if the file already exists
if (!file.Exists)
{
attachment.Save(file);
}
}
}