Monday 23 December 2013

JavaScript - Generate an array of numbers in between range

<script type="text/javascript">
        var numbers = [];
        min = 0;
        max = 100;
        for (var i = min; numbersArray.push(i++) < max;);
        // numbers = [0,1,2,3 ... 100] 
 </script>

Sunday 15 December 2013

C#.NET - Sending email through Gmail SMTP server in ASP.NET

Hi welcome to dotnetcookie.blogspot.in

In this article I will show you how to send an email through the Gmail SMTP server.

In this article we should use Gmail address (sample@gmail.com) and password (*******) to send an email.


SendMail.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendMail.aspx.cs" Inherits="SendMail.SendMail" %>

<html>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h3>Sending Email in C#.NET using gmail.</h3>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Email</span>
                </td>
                <td>
                    <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Password</span>
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    <span>To</span>
                </td>
                <td>
                    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Subject</span>
                </td>
                <td>
                    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Body</span>
                </td>
                <td>
                    <asp:TextBox ID="txtBody" TextMode="MultiLine" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:Button ID="btnSendEmail" runat="server" Text="SendMail" OnClick="btnSendEmail_Click" />
                </td>
            </tr>
        </table>

    </form>
</body>
</html>

Form


SendMail.aspx.cs

namespace SendMail
{
    public partial class SendMail : System.Web.UI.Page
    {
        protected void btnSendEmail_Click(object sender, System.EventArgs e)
        {
            try
            {
                // retrive all the details from the page.
                var fromAddress = txtFrom.Text;
                var toAddress = txtTo.Text;
                string fromPassword = txtPassword.Text;
                string subject = txtSubject.Text;
                string body = txtBody.Text;
 
                // Create a new SMTP Client instance by passing the SMTP details.
                var smtp = new System.Net.Mail.SmtpClient();
                {
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    smtp.Credentials = new System.Net.NetworkCredential(fromAddress, fromPassword);
                    smtp.Timeout = 20000;
                }
 
                //Send the email using the send method of the System.Net.Mail.SmtpClient
                smtp.Send(fromAddress, toAddress, subject, body);
                Response.Write("<script>alert('Mail Sent Successfully...')</script>");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Response.Write("<script>alert('The username or password you entered is incorrect.')</script>");                
            }
            catch (System.Exception ex)
            {
                Response.Write("<script>alert('"+ex.Message+"')</script>");               
            }
            finally
            {
                ResetControls();
            }
        }
        
        private void ResetControls()
        {
            txtFrom.Text = string.Empty;
            txtTo.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtSubject.Text = string.Empty;
            txtBody.Text = string.Empty;
        }
    }
 
}

Form


Wednesday 11 December 2013

HTML/CSS/jQuery - Show hide div when select option is selected


Hi in this small article i will show you how to show hide div when select (HTML Combobox) option is selected.

1. Create a html page.
2. Add reference to jQuery script.
3. Design the page with one combobox and one div.
4. Write the jQuery script to show and hide the div when select (HTML Combobox) option is selected.

Use the following script to to show and hide the div when select (HTML Combobox) option is selected.

HTML

<html>
<head>
    <!--load jQuery into your page by giving reference-->
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //Chage function call when you change the combobox value
            $("#action").change(function () {               
                var value = $("#action").val();
                //Show and hide the div tag based on the selected value of combobox.
                if (value == 'show') {
                    $("#divShowHide").show();
                }
                else {
                    $("#divShowHide").hide();
                }

            });
        });
    </script>
</head>
<body>
    Select Action:
    <select id="action">
        <option value="show">show</option>
        <option value="hide">hide</option>
    </select>
    <div id="divShowHide" style="height: 100px; width: 300px; background-color: gray; padding: 10px;">
        Sample Text
    </div>
</body>
</html>


Design




Design