Get email address for mail.to.add from a SQL Server stored procedure in C# application

s2j5cfk0  于 2023-11-16  发布在  SQL Server
关注(0)|答案(1)|浏览(157)

I want to send an email to a user after they submit a form in my C# application. Their email address is stored in the database and I want to create a stored procedure that will pick their email address according to their man no.

This is my working code so far but it only applies to already listed email addresses.

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

SmtpClient SmtpServer = new SmtpClient("smtp.office365.com", 25);
SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 10000000;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;

SmtpServer.Credentials = new System.Net.NetworkCredential(email, password, domain);

MailMessage mail = new MailMessage();
mail.From = new MailAddress(email);
mail.To.Add(""); **//Want this to come calling a stored procedure**
mail.Subject = "MAIL TEST";
mail.Body = "Mail code working";
mail.IsBodyHtml = true;

SmtpServer.Send(mail);
SmtpServer.Dispose();

SmtpServer.SendCompleted += (s, x) => {
    SmtpServer.Dispose();
    mail.Dispose();
};

WebMessageBox.Show("Mail sent");
rbpvctlc

rbpvctlc1#

You would need to create a C# part and a tSQL part to make this work.

C#

//Create a new function that returns a string.
private string GetEmailAddresFromDB(int ManNo) {
    //sqlText is the name of the stored procedure in SQL server.
    string sqlText = "sp_GetEmailAddressFromManNo";

    //using blocks for the connection and command so they are properly disposed when done.
    //You have to provide a valid connection string.
    using (SqlConnection conn = new SqlConnection("sqlconnectionstring")) {
        conn.Open();    //Open the connection.
        using (SqlCommand cmd = new SqlCommand(sqlText, conn)) {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ManNo", SqlDbType.Int, -1) { Value =  ManNo });
            cmd.Parameters.Add(new SqlParameter("@EmailAddress", SqlDbType.NVarChar, 255) { Direction = ParemeterDirection.Output });

            cmd.ExecuteNonQuery();
            return cmd.Parameters["@EmailAddress"].Value.ToString(); //Read the output parameter and return.
        }
    }
}

SQL

--This stored procedure uses an output parameter to send the email address back to code.
--If you want to return more than just an email address, 
--  you should return a table result and use some kind of 
--  DataAdapter in C# to read the relevant table data from the result.
CREATE PROCEDURE sp_GetEmailAddressFromManNo 
    @ManNo int
    , @EmailAddress nvarchar output
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SELECT @EmailAddress = email_address
    FROM users
    WHERE man_no = @ManNo
END

And then modify your code as such:

mail.To.Add(GetEmailAddressFromDB(ManNo: manNo));

This is all pseudo code. You'll have to modify the Store Procedure and C# function to match your parameters, store procedure name, and other unknowns which you haven't shown us. You might also want to add some error handling using try {} catch {} blocks.

相关问题