Friday 19 December 2014

How to send email in asp.net using c# through gmail

Hello friends,
                    Now I am  going to describe step by step how to sending  message through  gmail using asp.net in c# .
Step-1:
         First  we should design frontend like this using following codes. in .aspx page(like-smssend.aspx)




Code of .aspx page for design frontend like above screenshot:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            height: 45px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table
        style="width: 27%; border: 1px solid #FF0000; height: 193px;">
        <tr>
            <td align="center" colspan="2"
                style="color: #FF0000; font-weight: bold; font-size: medium">
                <br />
                Send message through email in asp.netusing C#</td>
        </tr>
        <tr>
            <td>
                Name</td>
            <td>
                <asp:TextBox ID="txtName" runat="server" ></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style1">
                Email</td>
            <td class="auto-style1">
                <asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox>
                       </td>
        </tr>
        <tr>
            <td>
                Subject</td>
            <td>
                <asp:TextBox ID="txtsubject" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Message</td>
            <td>
                <asp:TextBox ID="txtmsg" runat="server" Height="58px" TextMode="MultiLine"
                    Width="422px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2">
                <asp:Button ID="btnsubmit" runat="server"  Text="Submit" OnClick="btnsubmit_Click1" />
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2">
                <asp:Label ID="lblmessage" runat="server" ForeColor="Red"></asp:Label>
            </td>
        </tr>
        </table>
    </div>
    </form>
</body>
</html>
Step-2:
  In 2nd step first we import this on top of the .aspx.cs page(like-smssend.aspx.cs)
using System.Net.Mail;
Than
   double click on submit button and write following code:

protected void btnsubmit_Click1(object sender, EventArgs e)
    {
        try
        {
            MailMessage message = new MailMessage();
            // Sender e-mail address.
            message.From = new MailAddress(txtEmail.Text);
            // Recipient e-mail address.
            message.To.Add("sentemail@gmail.com");
            message.Subject = txtsubject.Text;
            //Msg.Body = "<b>Contact Request From :</b>  <br/><br>Name: " + txtName.Text.Trim() + "<br/><br> Email Address: " + txtEmail.Text.Trim() + "<br/><br>Comments: " + TextBox5. Text + "<br>";
            // your remote SMTP server IP.
            message.Body = txtmsg.Text;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");
            smtp.EnableSsl = true;
            smtp.Send(message);
            //Msg = null;
            lblmessage.Text = "Your Message sent Sucessfully...";
            // Clear the textbox valuess
            txtName.Text = "";
            txtsubject.Text = "";
            txtsubject.Text = "";
            txtEmail.Text = "";
            txtmsg.Text = "";
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
    }


Thanks
Rabi








2 comments: