Monday 8 July 2013

How to send email in ASP.NET ?


In this article I will explain how to send email using asp.net

Properties explained:

SmtpServer: The name the SMTP server that will be used to send the emails.

SmtpPort: The port the server will use to send SMTP transactions (emails).

Add below two namespaces to your web page

using System.Net.Mail;
using System.Net;


protected void btnMail_Click(object sender, EventArgs e)
    {
            Mail();
    }

Below is the code Mail sending functionality code.

public void Mail()
    {
      
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("Enter from mail ID");
        mail.To.Add(new MailAddress("Enter To Mail ID"));
        mail.CC.Add(new MailAddress("Enter CC Mail ID");
        mail.Subject = "Order Details";
        StringBuilder sb = new StringBuilder();

        sb.Append("Dear Customer ,");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append(" Thank for your order.");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("Order no :<br />");      
        sb.Append("<br/>");
        sb.Append("<br/>");   
        sb.Append("<br/>");
        sb.Append("We will get back to you in case of any clarification required / post you a mail in case of discrepancy.");
        sb.Append("<br/>");
      
        Label Body = new Label();
        Body.Text = sb.ToString();

        mail.Body = Body.Text;
        mail.IsBodyHtml = true;
        mail.BodyEncoding = Encoding.UTF8;

        mail.Priority = MailPriority.High;
        SmtpClient smtp = new SmtpClient("Enter your SMTP Credentials");


        smtp.Port = Enter your port number;
        System.Net.NetworkCredential credentials = new NetworkCredential();
        smtp.Credentials = new System.Net.NetworkCredential("Enter your Mail id", "Password");
        smtp.EnableSsl = false;
        smtp.Send(mail);
    }

No comments:

Post a Comment