Total Pageviews

28 February 2013

How to Send smtp Mail with the help of asp.net with example



Web configuration=>Here we are setting the smtp port for sending mail in with spacific domain there are several steps are given as blow-
Step First-
<system.net>
    <mailSettings>
      <smtp>
        <network host="smtp.domain.com" port="25" userName="domain id" password="domain password" />
      </smtp>
    </mailSettings>
  </system.net>
  <appSettings>
    <add key="amrit" value="ID@domain.com"/>
  </appSettings>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
Step Second-Here we sre designing in Asp.net page.
<div>
To<asp:Textbox id="TextBox3.Text" runat="server" />
Subject<asp:Textbox id="TextBox1.Text" runat="server" />
Body<asp:Textbox id="TextBox2.Text" runat="server" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
            Text="Send Mail" />
</div>
Step Third-
using System.Net.Mail;
 protected void Button2_Click(object sender, EventArgs e)
    {
string fromEmail = ConfigurationManager.AppSettings["amrit"].ToString();
        string toEmail = TextBox3.Text;

        MailMessage message = new MailMessage(fromEmail, toEmail);
        message.Subject = TextBox1.Text;
        message.Body = TextBox2.Text;
       
        SmtpClient smtp = new SmtpClient();
        smtp.Send(message);
}

No comments:

Post a Comment

Contact Form

Name

Email *

Message *