C# Jadu
  • Home
  • Asp.net
  • Web Design
    • Html
    • Css
    • Java Script
  • Bootstrap
  • Sql
    • Queries
    • Stored Procedures
  • About Me
    • About Myself
    • My Projects
    • My CV
  • Gallery
  • Do you Know?

Thursday, 24 September 2015

how to send Email using Asp.net?

 Deepak Chaudhary     11:44     ASP.NET     No comments   

Hi Friends...


Here I explain full procedure step by step 

Step 1: Design a page using this code



<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Send Mail</title>
    <style type="text/css">
        .auto-style1 {
            height: 20px;
        }
    </style>
</head>
<body bordercolor="#6600FF">
    <br />
    <form id="form1" runat="server">
        <div>
            <table style="border: 3px solid #db6b11; padding: 20px;" align="center">
                <tr>
                    <td colspan="2" align="center" class="auto-style1">
                        <p><font color="#db6b11" style="font-size: 25px;">C# JADU EMAIL SYSTEM </font></p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p><font color="#db6b11" style="font-size: 25px;">Gmail Username:&nbsp;-&gt;&nbsp;&nbsp;&nbsp; </font></p>
                    </td>
                    <td>
                        <asp:TextBox ID="txtUsername" runat="server" BackColor="White" ForeColor="#db6b11"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p><font color="#db6b11" style="font-size: 25px;">Gmail Password:--&gt;</font></p>
                    </td>
                    <td>
                        <asp:TextBox ID="txtpwd" runat="server" TextMode="Password" BackColor="White" ForeColor="#db6b11"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p><font color="#db6b11" style="font-size: 25px;">Subject:-------------&gt;</font></p>
                    </td>
                    <td>
                        <asp:TextBox ID="txtSubject" runat="server" BackColor="White" ForeColor="#db6b11"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p><font color="#db6b11" style="font-size: 25px;">To:-------------------&gt;</font></p>
                    </td>
                    <td>
                        <asp:TextBox ID="txtTo" runat="server" BackColor="White" ForeColor="#db6b11"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p><font color="#db6b11" style="font-size: 25px;">Enter message:</font></p>
                    </td>
                    <td>
                        <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" BackColor="White" ForeColor="#db6b11" Height="197px" Width="207px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Button ID="btnSubmit" Text="Send" runat="server" OnClick="btnSubmit_Click"                                BackColor="#db6b11" ForeColor="#FFFFFF" Height="32px" Width="63px" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


Your design will look like this



Step 2: Add some header files



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Security;
using System.Net.Mail;

Step 3:   c# page code goes here


public partial class email : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            // Sender e-mail address.
            Msg.From = new MailAddress(txtUsername.Text);
            // Recipient e-mail address.
            Msg.To.Add(txtTo.Text);
            Msg.Subject = txtSubject.Text;
            Msg.Body = txtBody.Text;
            // your remote SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            Msg = null;
            Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
    }
}

Demo:


C# jadu







  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Most Popular posts

  • How To Create Image wipe Effect With CSS3 Webkit Animation
    Hi Friends... In this article we learn how to create a Image wipe effect with CSS3. CSS transition and animations provide a way for web de...
  • CSS 3D Cube
    Hi Friends... CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a lang...
  • Interesting facts about India | Aware Talk
    1. Shampooing is an Indian concept Shampoo was invented in India, not the commercial liquid ones but the method by use of herbs. ...

About Me

Deepak Chaudhary
View my complete profile

Copyright © C# Jadu | Powered by Blogger
Design by Deepak Chaudhary