C # calls SQL Server sp_send_dbmail stored procedure to send mail

Source: Internet
Author: User

<appSettings>
<add key= "Mailprofile" value= "AAA"/> </appSettings>

private void Btnsendmail ()
{
Try
{
String mailprofile = configurationmanager.appsettings["Mailprofile"];

               string receivers = "[email protected]";
String cc = "";
string connectionString = configurationmanager.connectionstrings["BB"]. ConnectionString;
String subject = "Subject";
string fileattachments = string. Empty;
string BODY = string. Empty;

\ r indicates carriage return, \ n the line is generally \ r \ n
BODY = "Hi gugs:\r\n\r\n";
BODY = body + "\r\n\r\n Source:" + comsource.text;
BODY = body + "\r\n\r\n Thanks";

Initialize the parameters of the stored procedure

var parammailprofile = new SqlParameter ("@profile_name", SqlDbType.NVarChar, +) {Value = Mailprofil e};
var paramreceivers = new SqlParameter ("@recipients", SqlDbType.NVarChar,-1) {Value = receivers};
var paramcc = new SqlParameter ("@copy_recipients", SqlDbType.NVarChar, 255) {Value = cc};
var parambody = new SqlParameter ("@body", SqlDbType.NVarChar,-1) {Value = body};
var paramsubject = new SqlParameter ("@subject", SqlDbType.NVarChar, 255) {Value = subject};
var paramfileattachments = new SqlParameter ("@file_attachments", SqlDbType.NVarChar,-1) {Value = fil Eattachments};

using (var conn = new SqlConnection (connectionString))
using (var cmd = new SqlCommand ("Msdb.dbo.sp_send_dbmail", conn)
                {
CommandType = CommandType.StoredProcedure,
CommandTimeout =.
                })
                {
cmd. Parameters.addrange (new[] {parammailprofile, paramreceivers, PARAMCC, Parambody, Paramsubject, paramFileAttachments} );
Conn. Open ();
cmd. ExecuteNonQuery ();
                }
            }
catch (Exception ex)
            {
            return;
            }
        }

C # calls SQL Server sp_send_dbmail stored procedure to send mail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.