C# - 将数据库中保存的文件附加到电子邮件中

标签 c# sql file-upload email-attachments binary-data

如何将 MS SQL 数据库中保存的一个或多个文件添加到电子邮件中?我已经知道如何附加保存在目录中或来自 fileUpload 控件的文件。

我的数据字段如下:

col1:Email_ID - 整数

col2:文件名 - varchar

col3:file_content - 图像

所以我的 SQL select 语句非常简单:

Select file_name, file_content from Attachments where Email_ID = 333

我只是不知道如何将它们附加到我的电子邮件中。

谢谢!

最佳答案

  SqlCommand cmdSelect=new SqlCommand("Select file_name, file_content " + 
              " from Attachments where 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 you can attache you file to email here your file is generate at path stored in "strfn" variable
}

引用文献:http://www.codeproject.com/KB/database/ImageSaveInDataBase.aspx

关于C# - 将数据库中保存的文件附加到电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6777168/

相关文章:

mysql - 如何解决MySQL中的汉字显示问题?

java - 使用分段上传图像时如何在客户端获取进程

通过重新启动修复加载数据 infile 的 MySQL 错误?

c# - parking 费计算

php - CREATE IDENTIFIER ----使用php将2个表单数据插入到一张表中

c# - 如何为各种代码块创建通用超时对象?

php - 从多个表中获取同名列

c# - File.copy() 方法不支持给定路径的格式错误

C# DataGridView 单元格格式化

c# - 在运行时在哪里存储密码?