How to send email in ASP.NET C# How to send email in ASP.NET C# asp.net asp.net

How to send email in ASP.NET C#


Just go through the below code.

SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25);smtpClient.Credentials = new System.Net.NetworkCredential("info@MyWebsiteDomainName.com", "myIDPassword");// smtpClient.UseDefaultCredentials = true; // uncomment if you don't want to use the network credentialssmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;smtpClient.EnableSsl = true;MailMessage mail = new MailMessage();//Setting From , To and CCmail.From = new MailAddress("info@MyWebsiteDomainName", "MyWeb Site");mail.To.Add(new MailAddress("info@MyWebsiteDomainName"));mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));smtpClient.Send(mail);


Try using this code instead. Note: In the "from address" give your correct email id and password.

protected void btn_send_Click(object sender, EventArgs e){    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();    mail.To.Add("to gmail address");    mail.From = new MailAddress("from gmail address", "Email head", System.Text.Encoding.UTF8);    mail.Subject = "This mail is send from asp.net application";    mail.SubjectEncoding = System.Text.Encoding.UTF8;    mail.Body = "This is Email Body Text";    mail.BodyEncoding = System.Text.Encoding.UTF8;    mail.IsBodyHtml = true;    mail.Priority = MailPriority.High;    SmtpClient client = new SmtpClient();    client.Credentials = new System.Net.NetworkCredential("from gmail address", "your gmail account password");    client.Port = 587;    client.Host = "smtp.gmail.com";    client.EnableSsl = true;    try    {        client.Send(mail);        Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");    }    catch (Exception ex)    {        Exception ex2 = ex;        string errorMessage = string.Empty;        while (ex2 != null)        {            errorMessage += ex2.ToString();            ex2 = ex2.InnerException;        }        Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");    }}


You can try this using hotmail like this:-

MailMessage o = new MailMessage("From", "To","Subject", "Body");NetworkCredential netCred= new NetworkCredential("Sender Email","Sender Password");SmtpClient smtpobj= new SmtpClient("smtp.live.com", 587); smtpobj.EnableSsl = true;smtpobj.Credentials = netCred;smtpobj.Send(o);