How to send email in ASP.NET- C# example

This is example how to send email from asp,net web application. Refer this post if you want to send email in java code .

This example will use google smtp email server.

The aspx Page

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Label ID="LErrorMessage" runat="server" ></asp:Label>
    <asp:Button ID="Button1" runat="server" Text="Send Email" OnClick="Button1_Click" />
</asp:Content>

The Code Behind

 protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
          
            string emailContent = "This mail is created and send through the c# code,"
                    + "\n\n if you are developers, visit http://www.developersnote.com!",
                    subject = "Developersnote update - Mail Send Using ASP.NET";
            string EmailFrom = "developersnote dot com",
                   EmailFromAddress = "developersnote.com@gmail.com";

            try
            {
                MailMessage mMsg = new MailMessage();
              
                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                smtpClient.EnableSsl = true;
                smtpClient.Credentials = new System.Net.NetworkCredential("your email address", "email password");
                mMsg.From = new MailAddress(EmailFromAddress, EmailFrom);
                mMsg.To.Add("developersnote@dev.com");
                mMsg.Subject = subject;
                mMsg.Body = emailContent;
                mMsg.IsBodyHtml = true;
                smtpClient.Send(mMsg);
              
            }
            catch(Exception ex)
            {
                LErrorMessage.Text = ex.Message;
            }
        }
    }

Sample receive email



By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico