sql - C# - Attach file saved in database to an email -
how can add 1 or more files saved in ms sql database email? know how attach files saved in directory or fileupload control.
my datable fields such:
col1: email_id - int
col2: file_name - varchar
col3: file_content - image
so sql select statement prety simple:
select file_name, file_content attachments email_id = 333
i can't figure how attach them emails afterwards.
thanks!
sqlcommand cmdselect=new sqlcommand("select file_name, file_content " + " attachments email_id=@id",this.sqlconnection1); cmdselect.parameters.add("@id",sqldbtype.int,4); cmdselect.parameters["@id"].value=333; datatable dt = new datatable(); this.sqlconnection1.open(); sqldataadapter sda = new sqldataadapter(); sda.selectcommand = cmdselect; sda.fill(dt); if(dt.rows.count > 0) { byte[] barrimg=(byte[])dt.rows[0]["file_content"]; string strfn= "your file directory path" + convert.tostring(dt.rows[0]["file_name"]); filestream fs=new filestream(strfn, filemode.createnew, fileaccess.write); fs.write(barrimg,0,barrimg.length); fs.flush(); fs.close(); //now can attache file email here file generate @ path stored in "strfn" variable }
references : http://www.codeproject.com/kb/database/imagesaveindatabase.aspx
Comments
Post a Comment