【问题标题】:How to send mails by attaching a file (file should be taken from Sql Server Database)如何通过附加文件发送邮件(文件应取自 Sql Server 数据库)
【发布时间】:2013-09-01 17:13:05
【问题描述】:

我创建了一个表

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 发送它

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

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

【问题讨论】:

    标签: asp.net sql-server


    【解决方案1】:
    【解决方案2】:

    您可能需要进行少量修改,但这样做就可以了。

    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();
            }
        }
    

    希望这会奏效。

    【讨论】:

    • 我在“btnsend_click”事件中调用函数时遇到问题 protected void btnsend_Click(object sender, EventArgs e) { SendEmail(); }
    • 错误:方法“SendEmail”没有重载采用“0”参数。
    • 所以我添加了这个代码“SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml);”在我的 btnsend_click() 事件中调用该函数
    • 只要去掉函数定义中的参数,在方法内部设置字符串toAddress、字符串ccAddress、字符串bccAddress、字符串主题、字符串正文、MailPriority优先级、bool isHtml的值。
    • 现在它正在工作,但这次我得到了运行时错误 System.Net.Mail.SmtpException: 操作已超时。在 System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.SendEmail() in e:\Web Application Projects\attachfilesfromdb\Default.aspx.cs:line 131
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2013-10-15
    • 2015-10-10
    • 2015-10-24
    相关资源
    最近更新 更多