在内存中创建多个文件并将它们附加到电子邮件中

本文关键字:电子邮件 创建 内存 文件 | 更新日期: 2023-09-27 18:09:27

我需要能够发送文本数据存储在一个数据库中的多行格式的文本文件附件中的电子邮件。想想不同的人在不同的时间添加的注释。

到目前为止,电子邮件发送并生成了一些文本文件,如test0.txt和test1.txt,但它们是空的。我冲洗流写入器,所以对我来说,它似乎文件应该有文本在它。我不能在发送电子邮件之前关闭流写入器,因为我收到了一个错误,说不能从已关闭的流中读取。我将内存流和流写入器都存储在容器中,所以它们不应该被处理或清理,直到最后。我想知道是否电子邮件对象丢失或由于某种原因无法访问存储在容器中的流?

我看过一些关于SO的类似问题,但它们似乎不起作用。

这个人正在使用byte[],所以只有内存流没有流写入器
这个人在给我发送错误的电子邮件之前处理他们的流写入器

而不是试图在内存中这样做,我应该只是写临时文件,然后包括那些作为附件?写入磁盘似乎很慢,这样我就可以从磁盘读取附件。
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[],然后使用在程序上将多个文件附加到电子邮件中概述的技术,而无需将其写入磁盘。下面是一些简单的代码让你开始:

var myString = "This is a test.";
var myBytes = System.Text.Encoding.UTF8.GetBytes(myString);

现在myBytesbyte s的数组。注意,您可能希望通过TransferEncoding属性指定附件的编码。