Send Email in asp.net  

by ne on 2021-09-29 under Asp.net

For sending emails in asp.net with c#.

first add the following namespace :-

using System.Net.Mail

and than you have to write following lines of code on button click event:-

    MailMessage message = new MailMessage();
    message.From = new MailAddress("fromusername@DomainName");
    message.To.Add(new MailAddress("tousername@DomainName"));
    message.CC.Add(new MailAddress("ccusername@DomainName"));
    message.Subject = "Subject";
    message.Body = "Content";
    SmtpClient client = new SmtpClient();
    client.Send(message);


in web.config you have to add following section:-

<system.net>
        <mailSettings>
            <smtp from="username@DomainName">
                <network host="SMTPServerName" port="25" userName="username" password="secret" defaultCredentials="true" />
            </smtp>
        </mailSettings>
    </system.net>

Thanks..