Popular Posts

Feb 20, 2014

Send mail using SMTP in PHP


Using PHP mailer you can send email usning SMTP.

<?php

function send_mail($mailAddress, $desc) {
    require_once('../phpmailer/class.phpmailer.php');
    include("../phpmailer/class.smtp.php");
    // optional, gets called from within class.phpmailer.php if not already loaded

    $mail = new PHPMailer();

    //$body             = file_get_contents('contents.html');
    //$body             = eregi_replace("[\]",'',$body);

    $mail->IsSMTP(); // telling the class to use SMTP
    //$mail->Host       = "10.48.1.45"; // Virtual SMTP server IP
    $mail->SMTPDebug = 2;                     // enables SMTP debug information (for testing)
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth = true;                        // enable SMTP authentication
    $mail->SMTPSecure = "tls";                    // sets the prefix to the servier
    $mail->Host = "ssl://smtp.gmail.com";      // sets GMAIL as the SMTP server
    $mail->Port = 587;                                 // set the SMTP port for the GMAIL server
    $mail->Username = "mail@gmail.com";  // GMAIL username Any ID of lafarge
    $mail->Password = "password";            // GMAIL password of the ID

    $mail->SetFrom('mail@domail.com', 'From Me'); // From whom the mail is sent
    $mail->AddReplyTo("mail@domail.com", "Hello Sera");

    $mail->Subject = "SMTP MAIL";
    $mail->Body = "You have successfully entered to sera's world " . "<a href='http://jubayerdevs.blogspot.com'>" . "Click here" . "</a>";
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

    $address = "mail@domail.com";
    if ($address == '') {
        echo "<script>alert('No mail address found for this user. Set a mail address please');</script>";
        die();
    }

    $mail->AddAddress($address, "Lafarge Procurement Software");

    //$mail->AddAttachment("examples/images/phpmailer.png");      // attachment
    //$mail->AddAttachment("examples/images/phpmailer_mini.gif"); // attachment

    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "<script>alert('Successfully Submitted');</script>";
    }
}
?>

Feb 19, 2014

Excel to Gridview





In .aspx file:  

<form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <br />
    <table>
    <tr><td><asp:FileUpload ID="testFileUpload" runat="server" /></td><td><asp:Button ID="btnUpload" runat="server" Height="21px" Text="Upload" Width="92px" onclick="btnUpload_Click" /></td></tr>
    </table>
    <br /> 
    <asp:GridView ID="dataGridView" runat="server">
    </asp:GridView>
    </div>
    </form>



In .cs file:


    string fileName = string.Empty;
    string fileExtension = string.Empty;
    string fileLocation = string.Empty;
    string getExcelSheetName = string.Empty;
    string connectionString = string.Empty;

    DataTable dtExcelSheetName = null;
    DataTable dtExcelRecords = null;

    OleDbConnection con = null;
    OleDbCommand cmd = null;
    OleDbDataAdapter dAdapter = null;


    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = new OleDbCommand();
        dtExcelRecords = new DataTable();
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (testFileUpload.HasFile)
            {
                fileName = Path.GetFileName(testFileUpload.PostedFile.FileName);
                fileExtension = Path.GetExtension(testFileUpload.PostedFile.FileName);
                fileLocation = Server.MapPath("~/App_Data/" + fileName);

                GetData(fileLocation);
                Label1.Text = "File is ok to upload.";
            }
        }
        catch (Exception ex)
        {
            Label1.Text = "File is not expected formated.";
        }
    }


    public void GetData(string loc)
    {
        testFileUpload.SaveAs(fileLocation);
        //Check whether file extension is xls or xslx

        if (fileExtension == ".xls")
        {
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + loc + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        else if (fileExtension == ".xlsx")
        {
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + loc + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }

        dtExcelRecords.Clear();
        dtExcelRecords.Reset();

        //Create OleDB Connection and OleDb Command

        con = new OleDbConnection(connectionString);

        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;
        dAdapter = new OleDbDataAdapter(cmd);

        con.Open();
        dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
        cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
        dAdapter.SelectCommand = cmd;
        dAdapter.Fill(dtExcelRecords);
        con.Close();

        dataGridView.DataSource = dtExcelRecords;
        dataGridView.DataBind();

    }

Jan 26, 2014

Encrypt/Decrypt String in c#




In .net you can easily encrypt/decrypt string.
The following code describes how to do:

    /// <summary>
    /// To encrypt the input password
    /// </summary>
    /// <param name="txtPassword"></param>
    /// <returns>It returns encrypted code</returns>
    public string EncryptPassword(string txtString)
    {
        byte[] passBytes = System.Text.Encoding.Unicode.GetBytes(txtString);
        string encryptPassword = Convert.ToBase64String(passBytes);
        return encryptPassword;
    }


    /// <summary>
    /// To Decrypt password
    /// </summary>
    /// <param name="encryptedPassword"></param>
    /// <returns>It returns plain password</returns>
    public string DecryptPassword(string encryptedString)
    {
        byte[] passByteData = Convert.FromBase64String(encryptedString);
        string originalPassword = System.Text.Encoding.Unicode.GetString(passByteData);
        return originalPassword;
    }



You can do it in another way:


        /// <summary>
    ///
    /// </summary>
    /// <param name="password"></param>
    /// <returns>It returns encyped string</returns>
    public string Encryptdata(string password)
    {
        string strmsg = string.Empty;
        byte[] encode = new
        byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        strmsg = Convert.ToBase64String(encode);
        return strmsg;
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="encryptpwd"></param>
    /// <returns>It returns text string</returns>
    public string Decryptdata(string encryptpwd)
    {
        string decryptpwd = string.Empty;


        UTF8Encoding encodepwd = new UTF8Encoding();
        System.Text.Decoder utf8Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new String(decoded_char);
        return decryptpwd;
    }