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

时间:2016-09-29 16:27:46

标签: c# .net email-attachments memorystream

我需要能够以多行格式将存储在数据库中的文本数据作为电子邮件中的txt文件附件发送。 Think Notes在不同的时间被不同的人添加到了某些东西中。

截至目前,电子邮件已发送,并且生成了一些文本文件,如test0.txt和test1.txt,但它们是空的。我正在冲洗流写器,所以对我来说,文件似乎应该包含文本。在发送电子邮件之前我无法关闭流编写器,因为我收到一条错误消息,说无法从封闭流中读取。我将内存流和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(); }

1 个答案:

答案 0 :(得分:1)

您可以简单地接受字符串并将其编码为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属性指定附件的编码。