数据库表中存在两个附件,均以二进制流的形式存放,现在想将这两个附件直接从数据库中取出,并且以邮件附件的形式发送出去。这些在程序里实现
关键代码 
byte[] byteAtach=(byte[])ds.Tables[0].Rows[0]["FileContent"];//读取二进制流
                                        FileStream fs = new FileStream(ds.Tables[0].Rows[0]["FileName"].ToString(), FileMode.OpenOrCreate);
                                        BinaryWriter bw = new BinaryWriter(fs); 
                                        bw.Write(byteAtach, 0, byteAtach.Length);
                                        bw.Close();
                                        fs.Close();
                                        Attachment data = new Attachment(fs,ds.Tables[0].Rows[0]["FileName"].ToString(), ds.Tables[0].Rows[0]["FileType"].ToString());但是问题是这么做每次都会在某个路径下创建一个附件的文件,我并不希望这样,我只希望将该文件读取出来,直接创建邮件的附件,不要生成多余的文件,请高人指点。

解决方案 »

  1.   

    看new Attachment有没有重载,可以用文件流格式做参数。
    如果没有,只能这样了,发送后把临时删除生成的文件删除。
      

  2.   

    可以先读入内存,然后Base64编码加入邮件。
    前提就是附件不是很大,你熟悉EMail格式或发送邮件支持EMail编码。
      

  3.   

    new Attachment有个Stream重载的
      

  4.   

    自己解决,将关键代码贴出来大家分享一下,多谢大家的支持
     byte[] byteAtach = (byte[])ds.Tables[0].Rows[0]["FileContent"];
      MemoryStream ss1 = new MemoryStream(); 
                                            
    ss1.Write(byteAtach, 0, byteAtach.Length);
    ss1.Seek(0,SeekOrigin.Begin);  Attachment att = new Attachment(ss1, "附件");
    Email.Attachments.Add(att);