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


show pop up JavaScript message through C#

Add this following script in html code :



<script>
        function ShowMessage() {
            alert('Thank you for your submission!  To complete, please continue with PayPal transaction');
            window.location.href = 'BuyNow.aspx';
        }
   
    </script>


C# Code :-

ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);