在内存中创建多个文件并将它们附加到电子邮件中
creating multiple in memory files and attaching them to an email
我需要能够以多行格式将存储在数据库中的文本数据作为电子邮件中的 txt 文件附件发送。想想不同的人在不同的时间添加到某物中的注释。
截至目前,电子邮件已发送,并且生成了一些文本文件,如 test0.txt 和 test1.txt,但它们是空的。我正在刷新 streamwriter,所以在我看来文件中应该有文本。我无法在发送电子邮件之前关闭流编写器,因为那时我收到一条错误消息,指出无法从已关闭的流中读取。我将 memorystream 和 streamwriter 都存储在容器中,这样直到最后它们才应该被处理或清理。我想知道电子邮件对象是否丢失或由于某种原因无法访问存储在容器中的流?
我在 SO 上看过一些类似的问题,但它们似乎不起作用。
This person is using byte[] so only memory stream no streamwriter
This person disposes of their streamwriter before sending email which errors out for me
与其尝试在内存中执行此操作,不如我只编写临时文件,然后将它们作为附件包含在内?写入磁盘似乎很慢,这样我就可以从磁盘读取以附加附件。
var companyEmail = new MailAddress("address@company.com", "Person Name");
email.To.Add(companyEmail);
email.From = new System.Net.Mail.MailAddress("donotreply@company.com", "doesn't matter");
email.Subject = "subject";
email.Body = "body";
email.IsBodyHtml = true;
var nonAttCounter = 0;
var nonAttStreamHolder = new List<MemoryStream>();
var nonAttWriterHolder = new List<StreamWriter>();
//churn through the attachments and see if any of them are checked in the form
foreach (DataRow datarow in claim.attachments.Rows)
{
string cbFormName = "ctl00$MainBody$att" + datarow["attNum"].ToString();//name of checkbox controls on page and in form.
var includedInForm = rForm[cbFormName];
//see if the attachment was selected as one to include.ie the attNum is in the posted form.
if (includedInForm != null)
{
string origData = datarow["origData"].ToString();
string[] fIDs = origData.Split(',');
foreach (var item in fIDs)
{
//not all attachments are real attachments with files...cause why would attachments be attachments.
int fid;
bool isInt = int.TryParse(item, out fid);
if (isInt)
{
var tempDS = new datastore(ref fid);
var tempData = tempDS.blobData;
email.Attachments.Add(new Attachment(tempData, tempDS.fileNameWithExt));
}
else
{
//grab all the textual data from the database for this "attachment" and write it to a memory stream and upload to the email
nonAttStreamHolder.Add(new MemoryStream());
nonAttWriterHolder.Add(new StreamWriter(nonAttStreamHolder[nonAttCounter]));
nonAttWriterHolder[nonAttCounter].WriteLine("This is a test.");
nonAttWriterHolder[nonAttCounter].WriteLine("Why this no work?!");
nonAttWriterHolder[nonAttCounter].Flush();
//nonAttWriterHolder[nonAttCounter].Close();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment tempFile = new System.Net.Mail.Attachment(nonAttStreamHolder[nonAttCounter], ct);
tempFile.ContentDisposition.FileName = "test" + nonAttCounter + ".txt";
email.Attachments.Add(tempFile);
nonAttCounter++;
}
}
}
}
Global_Utilities.SharedFunctions.emailQuickSend(email);
foreach (var writer in nonAttWriterHolder)
{ writer.Close(); }
foreach (var stream in nonAttStreamHolder)
{ stream.Close(); }
您可以简单地获取您的字符串并将它们编码为 byte[]
,然后使用 attach multiple files to an email programticaly without writing to disk 中概述的技术。这里有一些简单的代码可以帮助您入门:
var myString = "This is a test.";
var myBytes = System.Text.Encoding.UTF8.GetBytes(myString);
现在 myBytes
是 byte
的数组。请注意,您可能希望通过 TransferEncoding
属性.
指定附件的编码
我需要能够以多行格式将存储在数据库中的文本数据作为电子邮件中的 txt 文件附件发送。想想不同的人在不同的时间添加到某物中的注释。
截至目前,电子邮件已发送,并且生成了一些文本文件,如 test0.txt 和 test1.txt,但它们是空的。我正在刷新 streamwriter,所以在我看来文件中应该有文本。我无法在发送电子邮件之前关闭流编写器,因为那时我收到一条错误消息,指出无法从已关闭的流中读取。我将 memorystream 和 streamwriter 都存储在容器中,这样直到最后它们才应该被处理或清理。我想知道电子邮件对象是否丢失或由于某种原因无法访问存储在容器中的流?
我在 SO 上看过一些类似的问题,但它们似乎不起作用。
This person is using byte[] so only memory stream no streamwriter
This person disposes of their streamwriter before sending email which errors out for me
与其尝试在内存中执行此操作,不如我只编写临时文件,然后将它们作为附件包含在内?写入磁盘似乎很慢,这样我就可以从磁盘读取以附加附件。
var companyEmail = new MailAddress("address@company.com", "Person Name");
email.To.Add(companyEmail);
email.From = new System.Net.Mail.MailAddress("donotreply@company.com", "doesn't matter");
email.Subject = "subject";
email.Body = "body";
email.IsBodyHtml = true;
var nonAttCounter = 0;
var nonAttStreamHolder = new List<MemoryStream>();
var nonAttWriterHolder = new List<StreamWriter>();
//churn through the attachments and see if any of them are checked in the form
foreach (DataRow datarow in claim.attachments.Rows)
{
string cbFormName = "ctl00$MainBody$att" + datarow["attNum"].ToString();//name of checkbox controls on page and in form.
var includedInForm = rForm[cbFormName];
//see if the attachment was selected as one to include.ie the attNum is in the posted form.
if (includedInForm != null)
{
string origData = datarow["origData"].ToString();
string[] fIDs = origData.Split(',');
foreach (var item in fIDs)
{
//not all attachments are real attachments with files...cause why would attachments be attachments.
int fid;
bool isInt = int.TryParse(item, out fid);
if (isInt)
{
var tempDS = new datastore(ref fid);
var tempData = tempDS.blobData;
email.Attachments.Add(new Attachment(tempData, tempDS.fileNameWithExt));
}
else
{
//grab all the textual data from the database for this "attachment" and write it to a memory stream and upload to the email
nonAttStreamHolder.Add(new MemoryStream());
nonAttWriterHolder.Add(new StreamWriter(nonAttStreamHolder[nonAttCounter]));
nonAttWriterHolder[nonAttCounter].WriteLine("This is a test.");
nonAttWriterHolder[nonAttCounter].WriteLine("Why this no work?!");
nonAttWriterHolder[nonAttCounter].Flush();
//nonAttWriterHolder[nonAttCounter].Close();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment tempFile = new System.Net.Mail.Attachment(nonAttStreamHolder[nonAttCounter], ct);
tempFile.ContentDisposition.FileName = "test" + nonAttCounter + ".txt";
email.Attachments.Add(tempFile);
nonAttCounter++;
}
}
}
}
Global_Utilities.SharedFunctions.emailQuickSend(email);
foreach (var writer in nonAttWriterHolder)
{ writer.Close(); }
foreach (var stream in nonAttStreamHolder)
{ stream.Close(); }
您可以简单地获取您的字符串并将它们编码为 byte[]
,然后使用 attach multiple files to an email programticaly without writing to disk 中概述的技术。这里有一些简单的代码可以帮助您入门:
var myString = "This is a test.";
var myBytes = System.Text.Encoding.UTF8.GetBytes(myString);
现在 myBytes
是 byte
的数组。请注意,您可能希望通过 TransferEncoding
属性.