2016-09-29 25 views
0

我需要能够以多行格式将数据库中存储的文本数据发送为电子邮件中的txt文件附件。思考笔记在不同的时间被不同的人添加到某个东西上。在内存文件中创建多个文件并将它们附加到电子邮件

截至目前电子邮件发送,并有一些文本文件生成像test0.txt和test1.txt,但他们是空的。我正在冲洗Streamwriter对我来说,似乎文件应该有文本。在发送电子邮件之前,我无法关闭流式编写器,因为那时我收到一条错误,说无法从封闭流中读取。我将存储器流和Streamwriter存储在容器中,以便它们不应该被丢弃或清理到最后。我想知道电子​​邮件对象是否会因为某些原因而无法访问存储在容器中的流?

我看过一些类似的问题,但他们似乎并没有工作。

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("[email protected]", "Person Name"); 
email.To.Add(companyEmail); 

email.From = new System.Net.Mail.MailAddress("[email protected]", "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(); } 
+0

为什么你必须使用'StreamWriter'?你可以做与你连接的'byte []'问题相同的东西;只需使用'Encoding'将你得到的字符串转换为'byte []'... –

+0

@MikeMcCaughan那么简单明了。也许把它作为答案,我会将它标记为有史以来最好的答案! – Kevin

回答

1

你可以简单地把你的字符串(S)和编码它们作为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); 

现在myBytesbyte秒的阵列。请注意,您可能需要通过TransferEncoding属性指定附件的编码。

+0

感谢编码字节的想法。奇迹般有效。希望你可以和我的老板完全相信这个想法。 – Kevin

相关问题