Friday, December 9

Send mail through Smtp as gmail to any other mail

Source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
//Add this red color script from your solution or download it from google
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $(".flip").click(function () {
                $(".panel").slideToggle("slow");

            });


        });
        $(document).ready(function () {

            $("#txtName").click(function () {
              
                  
                    $(".panel").animate({ "height": "600" }, "slow");
                    $(".panel").animate({ "width": "1340" }, "slow");
              
            });


        });
    </script>
    <%--<script type="text/javascript">
        $(document).ready(function () {


            $(".ImageButton1").click(function () {

                $("#box").animate({ opacity: "0.1", left: "+=400" }, 1200)
        .animate({ opacity: "0.4", top: "+=160", height: "20", width: "20" }, "slow")
        .animate({ opacity: "1", left: "0", height: "100", width: "100" }, "slow")
        .animate({ top: "0" }, "fast")
        .slideUp()
        .slideDown("slow")
                return false;

            });

        });
</script>--%>
 <style type="text/css">
 div.panel,p.flip
 {
     margin:0;
     padding:5px;
  
   text-align:center;
    border:solid,1px,black; 
     }
     div.panel
     {
       
    
       display:none;
         background:gray;
         -moz-border-radius-topleft:40px;
-webkit-border-top-left-radius:30px;
-moz-border-radius-topright: 40px;
-webkit-border-top-right-radius:30px;
-moz-border-radius-bottomright: 40px;
-webkit-border-bottom-right-radius:30px;
-moz-border-radius-bottomleft: 40px;
-webkit-border-bottom-left-radius:30px;
         }

     .style1
     {
         width: 43%;
     }
     
     .style2
     {
         width: 194px;
     }
     
 </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
         <asp:Image ID="box" style="display:none;" runat="server" Height="31px"
             ImageUrl="~/images/imagesmsg.jpg" Width="60px" /><asp:label id="lblStatus" Runat="server" EnableViewState="False"></asp:label>
    
     </div>
     <div class="panel">
    <div class="col_w420 float_l">
         
             <h3>Swash Contact Form</h3>
             
              <div id="cp_contact_form">
            
             
                    <div align="center">
        <h2 style="background-color:Brown; color:Wheat; font-family:Verdana; font-size:14px" align=center>Please enter the following requested
information below to send us your Message.</h2>
<table align=center>
<tr>
<td class="style1">
                        <label for="author">
                      
Name:</label></td>
<td class="style2"><asp:textbox id="txtName" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td class="style1">
                        <label for="email">
                        Email:</label></td>
<td class="style2"><asp:textbox id="txtEmail" Width="241px" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td class="style1">
                        <label for="subject">
                        Subject:</label></td>
<td class="style2"><asp:textbox id="txtSubject" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td colSpan="2" >
                        <label for="text">
                        Message:</label></td>
</tr>
<tr>
<td align="center" colSpan="2"><asp:textbox id="txtMessage" Width="97%"
                            Runat="server" Height="99px" TextMode="MultiLine"
        MaxLength="400"></asp:textbox></td>
</tr>
<tr>
<td colSpan="2">&nbsp;</td>
</tr>
<tr>
<td align=center class="ImageButton1">
<%--<asp:button id="btnSendmail" Runat="server" Text="Send "
                            OnClick="btnSendmail_Click"></asp:button>
                            --%>
                           <asp:ImageButton ID="ImageButton1" runat="server"
         ImageUrl="~/images/images.jpg" Height="71px" Width="180px"
         onclick="btnSendmail_Click"  />
                            </td>
<td align=center class="style2">
<%--<asp:button id="btnReset" Runat="server" Text="Reset" OnClick="btnReset_Click"></asp:button>--%>

    

</td>
</tr>
<tr>
<td colSpan="2"></td>
</tr>
</table>
    </div>
  
            </div>
              
      </div>
      </div>
      <p style="background-image: url('images/percentagegradient.png'); border-radius:50px;" class="flip">
    Send a mail From www.swashglobal.com
 </p>
    </form>
</body>
</html>

My code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

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

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {

    }
 
    protected void btnSendmail_Click(object sender, ImageClickEventArgs e)
    {
        try
        {

            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            mail.From = new MailAddress(txtName.Text);
            // mail.To.Add(new MailAddress(txtTo.Text));
            mail.To.Add(new MailAddress(txtEmail.Text));

            mail.Subject = txtSubject.Text;
            mail.Body = txtName.Text + txtMessage.Text;
            mail.IsBodyHtml = true;
            smtp.Host = "smtp.gmail.com";




            smtp.EnableSsl = true;
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = txtName.Text;
            NetworkCred.Password = "Password123";
            //NetworkCred.UserName = "xyz@shibashish.com";
            // NetworkCred.Password = "Password123!";
            smtp.UseDefaultCredentials = true;

            smtp.Credentials = new System.Net.NetworkCredential(txtName.Text, "Password123");

            smtp.Port = 587;//this is Gmail port for e-mail
            //smtp.Port = 25;// this is default port no
            smtp.Send(mail);//send an e-mail

            lblStatus.Text = "Message Sended";
            Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Message Sended successfully');</script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
Thanks
shibashish mohanty



No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty