Home > Backend Development > C++ > How to Send Emails in ASP.NET C# Using SMTP?

How to Send Emails in ASP.NET C# Using SMTP?

Barbara Streisand
Release: 2025-01-17 15:46:11
Original
363 people have browsed it

How to Send Emails in ASP.NET C# Using SMTP?

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();
        }
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template