Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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);                       
        }
	}
Posted
Updated 19-Jan-18 1:33am
v2
Comments
Ram Nunna 19-Jan-18 5:45am    
I guess you need add @recipients value with "To email Id".
Could please add error message?

1 solution

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

It reads:
Quote:
[ @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.
 
Share this answer
 
Comments
Samira Radwan 19-Jan-18 8:10am    
Thanks for your answer. I know i have to get the absolute path of the attached file, the problem is the uploadFile control, the file is on the client/user machine I can't use Server.MapPath and i don't want to save the uploaded file (FileUpload.SaveAs). I'm looking for a way to upload PDF and send it as attachment using SQL database mail.
ZurdoDev 19-Jan-18 10:39am    
The file has to be somewhere that the sql server can access. There is no way around that.

But why is the file on the client's machine? Don't you have your website running on a server?
Richard Deeming 19-Jan-18 10:50am    
The path from the file upload control is the path, or sometimes just the name, of the file on the client.

If you want a path on the server, you have to save it somewhere on the server. :)
Samira Radwan 19-Jan-18 13:05pm    
Thank you both @011111100010‬ and @Richard Deeming for your valuable comments.
@011111100010‬ the user is going to upload a file from his/her machine using a web interface. I guess no choice I have to save the uploaded file first before sending as attachment.
ZurdoDev 19-Jan-18 13:59pm    
Correct. Just save it on the server somewhere, which is what uploading is doing anyway. Then you can send it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900