【发布时间】:2021-11-08 11:49:56
【问题描述】:
下面是我想要实现的,我下面的编码是我到目前为止所做的。
我不确定我正在做的是否是完成这项任务的最佳方法。
问题首先是动态创建一个 zip 文件夹并添加一个文件,以便我可以通过电子邮件发送它。
- 查询存储过程并获取数据
- 将返回的数据从存储过程转换为 CSV 文件。
- 动态创建一个 zip 文件夹并存储 CSV 文件。
- Zip 文件和 CSV 名称应该相同。 (名称将来自日期范围,例如“yyy-dd-mm”)
- 将 zip 文件夹附加到并通过电子邮件发送给客户。
- 最后将zip文件夹保存到本地目录或保存。
请告知是否有更好的方法来完成我的任务。
虚拟数据:
DataTable dummyData = new DataTable();
//columns
dummyData.Columns.Add("Id", typeof(int));
dummyData.Columns.Add("Make", typeof(string));
dummyData.Columns.Add("Model", typeof(string));
//data
dummyData.Rows.Add(111, "Toyota", "Hilux");
dummyData.Rows.Add(102, "Isuzu", "KB");
dummyData.Rows.Add(212, "Mazda", "323");
写入 CSV 文件,保存到路径并发送电子邮件:
if (dummyData != null && dummyData.Rows.Count > 0)
{
var fileName = "Testing11.csv";
StringBuilder sb = new StringBuilder();
string[] columnNames = dummyData.Columns.Cast<DataColumn>().Select(column => column.ColumnName).ToArray();
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dummyData.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
sb.AppendLine(string.Join(",", fields));
}
string newFile = @"C:\Testing\" + fileName + "";
// for testing purpose
if (File.Exists(newFile))
{
File.Delete(newFile);
}
// Write and save the file to directory.
File.WriteAllText(newFile, sb.ToString());
// Add file to Zip folder
// Not to sure how i can zip it
// Attach zip folder and send email.
StringBuilder emailText = new StringBuilder();
emailText.AppendLine("Good day,");
emailText.AppendLine("<br/>");
emailText.AppendLine("<br/>");
emailText.AppendLine("Please find attached copy of the file.");
emailText.AppendLine("<br/>");
emailText.AppendLine("<br/>");
emailText.AppendLine("Please don't hesitate to contact us if there is any discrepancies on the file.");
emailText.AppendLine("<br/>");
emailText.AppendLine("<br/>");
emailText.AppendLine("Regards,");
emailText.AppendLine("<br/>");
emailText.AppendLine(company);
emailText.AppendLine("<br/>");
emailText.AppendLine("<br/>");
emailText.AppendLine("This email was sent from an unattended email address!");
// Send an email to client
string emailSubject = newFile;
SendEmail(emailTo, emailSubject, emailText.ToString(),newFile);
}
else
{
throw new Exception("Not found!");
}
发送电子邮件:
public void SendEmail(string to, string subject, string body, string attachmentFileName = null)
{
try
{
SmtpClient client = new SmtpClient("mySTP",25)
{
DeliveryMethod = SmtpDeliveryMethod.Network
};
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("emailfrom@example.com");
foreach (string address in to.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
mailMessage.To.Add(address);
mailMessage.IsBodyHtml = true;
mailMessage.Body = body;
mailMessage.Subject = subject;
if (attachmentFileName != null)
{
mailMessage.Attachments.Add(new Attachment(attachmentFileName));
}
else
{
throw new Exception("Oops! No attachment file found");
}
client.Send(mailMessage);
}
catch (Exception ex)
{
throw ex;
}
}
【问题讨论】:
-
我想你需要这个 API 来完成你的任务docs.microsoft.com/en-us/dotnet/api/…
-
我的文件如何发送到 zip 文件夹
-
您必须创建临时文件夹并将文件保存在那里进行归档。或者您可以使用
ZipArchive并手动向其中添加条目以避免撞到文件系统。
标签: c# email datatable zip export-to-csv