Friday, January 13, 2012

Send Mail in C#


Create a simple aspx form :

// Use Validation
<asp:TextBox ID="txtName" runat="server" />
<asp:TextBox ID="txtFrom" runat="server" />                                  
<asp:TextBox ID="txtBody" runat="server"  TextMode="MultiLine"  />
<asp:Button ID="Btn_SendMail" Text="Send Mail" runat="server" onclick="Btn_SendMail_Click" />                                



C#

Add Namespace using System.Net.Mail;



 protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
           txtFrom.Text, "x@xxx.org ", "Message from" + txtName.Text.ToString(), txtBody.Text);
        SmtpClient SMTPServer = new SmtpClient("xxx.org");      // Check your SMTP server Name
        try
        {
            SMTPServer.Send(mailObj);
// show a success pop up
        }
        catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }
    }


No comments:

Post a Comment