【发布时间】:2018-08-24 10:31:33
【问题描述】:
我有一种方法可以创建一个 excel 文件并将其作为电子邮件附件发送。发送方法有效,但问题是它将excel转换为txt文件,打开时显示This file has been removed。
我使用 EPPLUS 库创建 excel 文件并将其保存在确定的位置。我可以毫无错误地打开创建的文件。
如何确保发送 excel 文件而不被“删除”?
创建excel文件的方法
private static string CreateExcel(List<BidModel> inputBids)
{
var fileName = string.Empty;
using (ExcelPackage excel = new ExcelPackage())
{
excel.Workbook.Worksheets.Add("Worksheet1");
var headerRow = new List<string[]>()
{
new string[] {"Zone", "Branch", "LC Number", "Acct No", "Customers Name", "Amount won"}
};
string headerRange = "A1:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "1";
var worksheet = excel.Workbook.Worksheets["Worksheet1"];
var stream = excel.Stream;
var count = inputBids.Count();
worksheet.Cells[headerRange].Style.Font.Bold = true;
worksheet.Cells[headerRange].LoadFromArrays(headerRow);
var fileN = string.Empty;
for (int i = 2; i <= count + 1;)
{
foreach (var item in inputBids)
{
fileN = item.Zone;
worksheet.Cells["A" + i].Value = item.Zone;
worksheet.Cells["B" + i].Value = item.Branch;
worksheet.Cells["C" + i].Value = item.LC_Number;
worksheet.Cells["D" + i].Value = item.Acct_no;
worksheet.Cells["E" + i].Value = item.Customer_name;
worksheet.Cells["F" + i].Value = item.Amount_won;
i++;
}
}
fileName = fileN.Trim().Replace(" ", string.Empty).Replace("/", string.Empty).Replace("-", string.Empty) + DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "_") + ".xlsx";
var path = ConfigurationManager.AppSettings["FilePath"];
//FileInfo excelFile = new FileInfo(path + fileName);
excel.SaveAs(new FileInfo(path + fileName));
bool exists = excel.File.Exists;
//created = true;
}
return fileName;
}
我创建的用于发送文件的方法
public static bool SendEmailAttachment(string fileName)
{
bool status = true;
string mailTo = ConfigurationManager.AppSettings["ToEmail"];
string subject = "MESSAGE";
try
{
using (MailMessage mail = new MailMessage(ConfigurationManager.AppSettings["FromEmail"], mailTo))
{
mail.Subject = subject;
mail.Body = "New Message";
mail.IsBodyHtml = true;
var path = ConfigurationManager.AppSettings["FilePath"] + fileName;
FileInfo file = new FileInfo(path);
ExcelPackage pkg = new ExcelPackage(file);
var stream = pkg.File.OpenRead();
//stream.Close();
Attachment attchment = new Attachment(stream, "name", mediaType: MediaTypeNames.Application.Octet);
attchment.ContentType = new ContentType("application/vnd.ms-excel");
//add attach
Attachment data = new Attachment(path);
data.ContentType = new ContentType("application/vnd.ms-excel");
//var attachment = CreateAttachment(WriteFileToMemory(path), fileName);
//mail.Attachments.Add(data);
mail.Attachments.Add(data);
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
//ServicePointManager.ServerCertificateValidationCallback =
//delegate (object s, X509Certificate certificate,
//X509Chain chain, SslPolicyErrors sslPolicyErrors)
//{ return true; };
smtp.Send(mail);
//Logwriter.WriteErrorLog("Email sent : account_no = " + accountNo);
}
}
catch (Exception ex)
{
status = false;
LogWriter.WriteTolog("Error sending email : " + ex.Message);
}
return status;
}
【问题讨论】:
-
在这种情况下您的问题是什么,请您详细说明
I have tried everything does'nt seem to work. -
The send method works but the issue i have is it converts the excel to a txt file and when you open it says This file has been removed.您使用的是哪个电子邮件客户端?您是否尝试过发送到其他电子邮件客户端?还是不同的电子邮件地址?这感觉像是“Outlook 喜欢阻止附件”的问题,而不是您的代码问题。 -
ershoaib 问题我可以得到解决为什么会发生这种情况,我使用 Outlook 客户端
-
您是否尝试过发送到其他电子邮件客户端?还是不同的电子邮件地址?