Asp.net fileupload作为SQL dbmail附件 [英] Asp.net fileupload as SQL dbmail attachment

查看:70
本文介绍了Asp.net fileupload作为SQL dbmail附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用SQL dbmail发送电子邮件,这对我很有用。

尝试从 fileUpload附加文件时在aspx页面中控制它会因文件无效而出错。



请告知使用dbmail将上传的文件作为附件发送应该怎么做。我知道使用System.Net.Mail非常简单,但要求是使用SQL数据库邮件



提前谢谢



我尝试过:



我试图使用上传文件的绝对路径或文件全名。

Hi All,
I'm trying to send an email using SQL dbmail which works very well for me.
When trying to attach file from fileUpload control in aspx page it gives an error as the file is invalid.

Please advise what should be done to send uploaded file as attachment using dbmail. I know using System.Net.Mail is pretty easy, but requirements is to use SQL database mail

Thanks in advance

What I have tried:

I tried to use absolute path of the uploaded file or the file full name.

if (uploadCV.HasFile)
    {
        FileInfo file = new FileInfo(uploadCV.FileName);
        if (file.Extension == ".pdf")
        {
            //send email
            string messageBody = "hello world";
            string subject = "Careers";
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString))
            {
				connection.Open();
                SqlCommand cmd = new SqlCommand("msdb.dbo.sp_send_dbmail", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@profile_name", SqlDbType.VarChar).Value = "profile";
                cmd.Parameters.Add("@body_format", SqlDbType.VarChar).Value = "HTML";
                cmd.Parameters.Add("@recipients", SqlDbType.VarChar).Value = "email"; 
                cmd.Parameters.Add("@subject", SqlDbType.VarChar).Value = subject;
                cmd.Parameters.Add("@body", SqlDbType.VarChar).Value = messageBody;
                //cmd.Parameters.Add("@file_attachments ", SqlDbType.NVarChar,-1).Value = uploadCV.FileName;
                //cmd.Parameters.Add("@copy_recipients",SqlDbType.VarChar).Value = ;
                //cmd.Parameters.Add("@blind_copy_recipients", SqlDbType.VarChar).Value = "";
                cmd.ExecuteNonQuery();
                connection.Close();
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('("+file.Name+") is not valid, only PDF format');", true);                       
        }
	}

推荐答案

请参阅文档sp_send_dbmail(Transact-SQL)| Microsoft Docs [ ^ ]。



内容如下:

Refer to the documentation, sp_send_dbmail (Transact-SQL) | Microsoft Docs[^].

It reads:
Quote:

[@ file_attachments =]'file_attachments'

是一个以分号分隔的文件名列表,用于附加到电子邮件中。必须将列表中的文件指定为绝对路径。附件列表的类型为nvarchar(max)。默认情况下,数据库邮件将文件附件限制为每个文件1 MB。

[ @file_attachments= ] 'file_attachments'
Is a semicolon-delimited list of file names to attach to the e-mail message. Files in the list must be specified as absolute paths. The attachments list is of type nvarchar(max). By default, Database Mail limits file attachments to 1 MB per file.


这篇关于Asp.net fileupload作为SQL dbmail附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆