Pages

Thursday, August 15, 2013

How to send mail from Gmail address to any mail address using C# code and SMTP.

It is easy to send mail from your email account to another email using gmail smtp server, Here the sample code in C#,  you should have to add the following class files in includes,

using System.Net.Mail;
using System.Net;

Here is the complete code,

            MailMessage mail = new MailMessage();
            string from = "fromemailaddress@gmail.com"; // your gmail address
            string pass = "yourpassword";  // your gmail password
            mail.From = new MailAddress(from);
            mail.To.Add("tomailaddress@gmail.com");  //to mail address
            mail.Subject = "This is test mail from smtp";
            mail.Body = "This is my test mail sending from SMTP ";
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.EnableSsl = true;
            smtp.Credentials = new NetworkCredential(from, pass);
            try
            {
                smtp.Send(mail);

            }
            catch (Exception ex)
            {
                throw new System.Exception(ex.Message);
            }

Thursday, July 25, 2013

How do validate Email address using jQuery?

Here is example for validating user email id using jquery

HTML
<input type="text" id="email"></input> 
<input type="button" value="test" id="click"/>

jQuery
$("#click").click(function (e) {
 var emailReg = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;;
  if( !emailReg.test( $("#email").val() ) ) {
    alert("Invalid email id");  
    return false;
  } else {
    alert("Valid email id");  
    return true;
  }    
});

Thursday, June 27, 2013

How to dynamically add a CSS class for an HTML element

Here is another wonderful feature of jQuery, you can add or remove CSS class dynamically using jQuery addClass() and removeClass() methods. For example, if you want to change the background color of a DIV element for a bit of time, you can add that CSS class property on the fly.
see the example below,
HTML
<div id ="notice" class="">Please notice this area</div>
<button>Add Class</button>
jQuery
$("button").click(function(){
    $("#notice").addClass("divcls");
    setTimeout(function() { $("#notice").removeClass("divcls") }, 1000);
 });
CSS
.divcls{background-color:red;font-weight:bold;width:150px}

Tuesday, June 25, 2013

How to read data from a JSON file using jQuery (getJSON() method)

Here is the example how to read a simple JSON file data using jQuery getJSON() method,

Sample JSON file structure, 

{
"news1": "This is a first sample news feed",
"news2": "This is a second sample news feed",
"news3": "This is a third sample news feed"
}
this file you can store as .js or .json extension or even .txt (for this example, I store it as .js extension in script folder)

HTML 
<ul>
  <li><div id="news1" style="height:100px;width:auto"></div> </li>
  <li><div id="news2" style="height:100px;width:auto"></div></li>
  <li><div id="news3" style="height:100px;width:auto"></div> </li>
</ul>
jQuery
<script type="text/javascript">
 $(document).ready(
  function() {
      $.getJSON('script/news1.js', function(jd) {
   $('#news1').append('<p>' + jd.news1 + '</p>');
   $('#news2').append('<p>' + jd.news2 + '</p>');
   $('#news3').append('<p>' + jd.news3 + '</p>');  
   });  
 });
</script> 

Monday, June 24, 2013

Cloning an HTML element using JQuery Clone() function.

Its just  an another amazing feature of JQuery. You can simply duplicate an HTML element using the Clone() function. for example, if you want to add new Table Row dynamically in to a Table, user can use Clone() function. Here is an example code,

HTML
<form>
    <table border="1" id="experiance">
        <tr>
            <th>Sl No</th>
            <th>Language</th>
            <th>Experiance</th>
            <th>Proficient</th>
        </tr>
        <tr>
            <td> <input type="text" /> </td>
            <td> <input type="text" /></td>
            <td> <input type="text" /></td>
            <td>
                <select>
                     <option value = "1">Beginner</option>/>
                     <option value = "2">Itermediate</option>/> 
                     <option value = "3">advanced</option>/>
                </select> 
            </td>
        </tr>
    </table>
    <input id="add" type="button" value="Add New Row" />
</form>
JQuery
$("#add").on('click', function () {
    var rows = 1;    
    var lastRow;
    lastRow = $('#experiance tr').last().clone().appendTo("#experiance");      
});
DEMO