数据库表中存在两个附件,均以二进制流的形式存放,现在想将这两个附件直接从数据库中取出,并且以邮件附件的形式发送出去。这些在程序里实现
关键代码
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());但是问题是这么做每次都会在某个路径下创建一个附件的文件,我并不希望这样,我只希望将该文件读取出来,直接创建邮件的附件,不要生成多余的文件,请高人指点。
关键代码
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());但是问题是这么做每次都会在某个路径下创建一个附件的文件,我并不希望这样,我只希望将该文件读取出来,直接创建邮件的附件,不要生成多余的文件,请高人指点。
如果没有,只能这样了,发送后把临时删除生成的文件删除。
前提就是附件不是很大,你熟悉EMail格式或发送邮件支持EMail编码。
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);