Sending Email in ASP.NET C#
If you're new to ASP.NET and looking to send emails, understanding the sender/receiver SMTP concept is crucial. SMTP (Simple Mail Transfer Protocol) defines the rules for email transmission.
Code Sample:
Consider the following code that utilizes the correct SMTP address provided by your ISP:
using System; using System.Net.Mail; public partial class SendMail : System.Web.UI.Page { protected void Btn_SendMail_Click(object sender, EventArgs e) { MailMessage mailObj = new MailMessage( txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text); SmtpClient SMTPServer = new SmtpClient("smtp-proxy.tm.net.my"); // Replace the hardcoded credentials with your own credentials SMTPServer.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword"); SMTPServer.Port = 25; // Adjust the port number according to the SMTP server's configuration (e.g., Google uses port 587) SMTPServer.EnableSsl = true; // This will depend on the SMTP server's security requirements try { SMTPServer.Send(mailObj); } catch (Exception ex) { Label1.Text = ex.ToString(); } } }
By using the correct SMTP address and port, specifying your credentials, and enabling SSL encryption if required, you can successfully send emails from your ASP.NET C# application.
The above is the detailed content of How to Send Emails in ASP.NET C# Using SMTP?. For more information, please follow other related articles on the PHP Chinese website!