First, add the namespace using system. net. Mail; using system. Data. sqlclient; Code In fact, it is very simple. Of course, my function is relatively simple, that is, a click event.
Protected Void Btn_send_click ( Object Sender, eventargs E)
... {
Mailmessage message = New Mailmessage ();
// Get the sender address
Message. From = New Mailaddress (tbx_address.text.trim ());
// Obtain a topic
Message. Subject = Tbx_subject.text.trim ();
// Obtain the subject of the sent Email
Message. Body = We_content.text.trim ();
Message. isbodyhtml = True ;
// Set database links
Sqlconnection Conn = New Sqlconnection (getstring ());
Conn. open ();
// Data Query uses stored procedures.
Sqlcommand cmd = New Sqlcommand ( " Selectemail " , Conn );
Cmd. commandtype = Commandtype. storedprocedure;
Sqlparameter dutyparameter = Cmd. Parameters. Add ( " @ Memberduty " , Sqldbtype. nvarchar, 20 );
Dutyparameter. Value = Ddl_selectemail.selectedvalue;
Sqldatareader Dr = Cmd. executereader ();
// Run the query to obtain the email address to be sent.
While (Dr. Read ())
... {
Message. to. Add (Dr ["Memberemail"]. Tostring ());
}
Conn. Close ();
Smtpclient Client = New Smtpclient ();
Client. Send (Message );
Lbl_info.visible = True ;
Lbl_info.text = " Sent successfully " ;
}
In addition, in the web. Config sets the remote server connection for sending emails
The Code is as follows:
< System.net >
< Mailsettings >
< SMTP >
< Network host = " Smtp.163.com " Port = " 25 " Username = " Your 163 account " Password = " Your password " />
</ SMTP >
</ Mailsettings >
</ System.net >