Tuesday, 28 January 2014

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

Leave a Comment
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 =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.