Blog : How to send email in asp.net C#

How to send email in asp.net C#



I'm very new to ASP.net C# area. I'm planning to send a mail thru asp.net C# and this is the smtp address from my ISP: smtp-proxy.tm.net.my

Below is what I tried to do but failed.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SendMail" %>
<html>
<head id="Head1" runat="server"><title>Email Test Page</title></head>
<body>
  <form id="form1" runat="server">
  Message to: <asp:TextBox ID="txtTo" runat="server" /><br>
  Message from: <asp:TextBox ID="txtFrom" runat="server" /><br>
  Subject: <asp:TextBox ID="txtSubject" runat="server" /><br>
  Message Body:<br>
  <asp:TextBox ID="txtBody" runat="server" Height="171px" TextMode="MultiLine"  Width="270px" /><br>
  <asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click" Text="Send Email" /><br>
  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  </form>
</body>
</html>

And below is my CodeBehind:

using System;
using System.Web.UI.WebControls;
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("127.0.0.1");
  try
  {
  SMTPServer.Send(mailObj);
  }
  catch (Exception ex)
  {
  Label1.Text = ex.ToString();
  }
  }
}

**P/S: I'm sorry that I couldn't understand the receiver/sender SMTP concept and so I am trying to understand the whole concept from here.


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;
  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
  smtpClient.EnableSsl = true;
  MailMessage mail = new MailMessage();


  //Setting From , To and CC
  mail.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:

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);