Amazon Ad

Friday 28 November 2014

Send Email To Any Email From Any Email

Hi Guys,

Following is the code for sending email to any user from any user. The below code is a good code and please do not misuse this.      

 MailMessage objMail = new MailMessage();
    //Replace email_of_the_user with the email whom you want to send to
            objMail.To.Add("email_of_the_user");
    //Replace email_to_be_cc'ed with the email whom you want to cc to
            objMail.CC.Add("email_to_be_cc'ed");
    //Replace Your_Email_ID with email id from whom you want the email to be sent from
            objMail.From = new MailAddress("Your_Email_ID", "Alias Name");

    //Subject of the email
            objMail.Subject = "Subject for the email";

            StringBuilder emailbody = new StringBuilder();
            emailbody.Append("Dear User,<br/><br/>");
            emailbody.Append("This is a test email <br/>");
            emailbody.Append("<br/><br/>Thanks<br/><b>Division of Infotech</b>");
            objMail.Body = emailbody.ToString();
            objMail.Priority = MailPriority.High;
            objMail.IsBodyHtml = true;
            System.Net.Mail.SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Timeout = 600000;
        //Replace EmailID with Email Id from where you want the email to be sent to
        //Replace Password with the email(email from) password
            smtp.Credentials = new System.Net.NetworkCredential("EmailID", "Password");
            smtp.EnableSsl = true;
            smtp.Send(objMail);
            objMail.Dispose();

Thanks
Ritesh

Wednesday 26 November 2014

Find Local and Internet IP Address in C#

Hi Guys,

I was given a task to get the IP Address of a computer. Most of the times when we are working we have two IP's one assigned on the LAN network and one assigned by the internet ISP. I was given a task to find both of them. Here is how i was able to create the same.

    //For ISP (Internet Service Provider) IP Address
    public static string GetIPAddress()
        {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            string sIPAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(sIPAddress))
            {
                return context.Request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                string[] ipArray = sIPAddress.Split(new Char[] { ',' });
                return ipArray[0];
            }
        }

    //For LAN (Local Area Network) IP Address
        public string LocalIPAddress()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                    break;
                }
            }
            return localIP;
        }

Thanks
Ritesh Tandon

Generate Password As Per Password Policy in C#

Hi Guys,

I was given a task to generate a password having following conditions

1. Min 10 Chracters
2. Must have one upper case letters
3. Must have one or more lower case letters
4. Must have one or more special characters
5. Must have one or more numbers.

Following is how i was able to do the same in C#

private string ActionNewPassword()
    {
        String Num = "0123456789";
        String Sp = "!@#$%&*()+{}[]^-";
        String SL = "abcdefghijklmnopqrstuvwxyz";
        String UCL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random rnd = new Random();
       
        //One or more upper case letters
        string part1 = UCL.Substring(rnd.Next(UCL.Length),1);

        //One or more numbers
        string part2 = Num.Substring(rnd.Next(Num.Length), 1);

        //Minimum 4 small case letters
        string part3 = "";
        for(int i=0;i<4;i++)
            part3 += SL.Substring(rnd.Next(SL.Length), 1);

        //Minimum 1 special character
        string part4 = Sp.Substring(rnd.Next(Sp.Length), 1);

        //Minimum 3 small numbers
        string part5 = "";
        for (int i = 0; i < 3; i++)
            part5 += SL.Substring(rnd.Next(SL.Length), 1);

        string pwd = part1 + part2+part3+part4+part5;

        //Message.InnerHtml = pwd;
       
        return pwd;
    }

Thanks
Ritesh Tandon

Generate Verification Code In MS SQL

Hi Guys,

I was given a task to generate an email verification code from MS SQL. I finally achieved with some  string functions and inbuilt Random function in MS SQL. Here is the code

    --Generate verification code
    declare @ab varchar(36)='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    declare @vercode varchar(11)=''
    declare @counter int=0
    declare @ind int=0
    WHILE @counter < 9
    BEGIN
        set @ind=(select RAND()*35)
        set @vercode=@vercode+(select SUBSTRING(@ab, @ind+1, 1))
        SET @counter = @counter + 1
    END
    print @vercode

The below code generates an alphanumeric code having 9 alphanumeric characters. You can increase or decrease the length by chaning the lenght of the @vercode variable and the while loop counter.

Thanks
Ritesh

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...