0

我创建了一个表

create table files
(
id int identity(1,1),
Name_of_file varchar(50) null,
interviewfile varbinary(max) Null,
)

将 .doc 文件插入到文件表中

insert into files 
select 'DB Creation' as filetype, *
from openrowset
(BULK 'E:\Office Works\Get SMS\DBA SMS Exam Stuff with 
DocumentationStandards\Interview Questions\Indexes-I.doc', SINGLE_BLOB)
as x

现在我想将“Indexes-I.doc”文件作为附件发送到特定邮件 ID。

我应该如何附加以及如何使用 Asp.Net 3.5 发送它

请告诉我解决方案............

谢谢和问候,文卡特·库马尔·奇古拉。

4

2 回答 2

0

您可能需要很少的修改,但这会做。

private void SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml)
    {
        try
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();

            MailAddress fromAddress = new MailAddress("sample@hotmail.com");

            smtpClient.Host = "xxx.xxx.com";
            smtpClient.Port = 25;

            message.From = fromAddress;
            message.Priority = priority;
            message.To.Add(toAddress);
            message.Subject = subject;
            if (ccAddress.Length > 0)
            {
                message.CC.Add(ccAddress);
            }
            if (bccAddress.Length > 0)
            {
                message.Bcc.Add(bccAddress);
            }

            message.IsBodyHtml = isHtml;
            message.Body = body;

            //load attachment from database
            sConn = ConfigurationManager.ConnectionStrings["eCIConnectionString"].ToString();
            try
            {
                sSQL = @"SELECT id, Name_of_file,  interviewfile" +
                        @"FROM files " +
                        @"WHERE Name_of_file = 'Your FileName'";

                conn.ConnectionString = sConn;
                conn.Open();
                cmd.CommandText = sSQL;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                cmd.Parameters.Clear();

                dAdapter.SelectCommand = cmd;
                DataTable dt = new DataTable();
                dAdapter.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        byte[] barrImg = (byte[])dt.Rows[i]["interviewfile"];
                        string strfn = Convert.ToString(dt.Rows[i]["Name_of_file"]);
                        FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
                        fs.Write(barrImg, 0, barrImg.Length);

                        fs.Flush();
                        fs.Close();
                        fs.Dispose();

                        Attachment att = new Attachment(strfn);
                        message.Attachments.Add(att);
                    }
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message + ex.StackTrace;
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    conn.Dispose();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
            }

            // Send SMTP mail
            smtpClient.Send(message);

            lblMessage.Text = "Email sent to " + toAddress + " successfully !";
        }
        catch (Exception ee)
        {
            lblMessage.Text = ee.ToString();
        }
    }

希望这会奏效。

于 2013-08-29T10:25:01.047 回答