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")
{
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.ExecuteNonQuery();
connection.Close();
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('("+file.Name+") is not valid, only PDF format');", true);
}
}