Sunday, August 18, 2013

Ramdom Password Generation - Asp.net C#

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

    }
    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        string allowedChars = "";
        allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
        allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";
        allowedChars += "1,2,3,4,5,6,7,8,9,0,!,@,#,$,%,&,?";

        char[] sep ={ ',' };
        string[] arr = allowedChars.Split(sep);
               
        string passwordString = "";

        string temp = "";

        Random rand = new Random();
        for (int i = 0; i < Convert.ToInt32(txtPassLength.Text); i++)
        {
            temp = arr[rand.Next(0, arr.Length)];
            passwordString += temp;
        }

        txtPassword.Text = passwordString;
    }
}

Open a new window - asp.net

<a href="#" onclick="window.open('new.aspx','New_Win','Height=400px,Width=400px' )''>view </a>

Email with attachment

using System;
02.using System.Data;
03.using System.Configuration;
04.using System.Web;
05.using System.Web.Security;
06.using System.Web.UI;
07.using System.Web.UI.WebControls;
08.using System.Web.UI.WebControls.WebParts;
09.using System.Web.UI.HtmlControls;
10.using System.Net.Mail;
11.
12.public partial class _Default : System.Web.UI.Page
13.{
14.    protected void Page_Load(object sender, EventArgs e)
15.    {
16.
17.    }
18.    protected void btnSend_Click(object sender, EventArgs e)
19.    {
20.        MailMessage mail = new MailMessage();
21.        mail.To.Add(txtTo.Text);
22.        //mail.To.Add("amit_jain_online@yahoo.com");
23.        mail.From = new MailAddress(txtFrom.Text);
24.        mail.Subject = txtSubject.Text;
25.        mail.Body = txtMessage.Text;
26.        mail.IsBodyHtml = true;
27.
28.        //Attach file using FileUpload Control and put the file in memory stream
29.        if (FileUpload1.HasFile)
30.        {
31.            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
32.        }
33.        SmtpClient smtp = new SmtpClient();
34.        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
35.        smtp.Credentials = new System.Net.NetworkCredential
36.             ("YourGmailID@gmail.com", "YourGmailPassword");
37.        //Or your Smtp Email ID and Password
38.        smtp.EnableSsl = true;
39.        smtp.Send(mail);
40.
41.    }

Send Email using Gmail - asp.net C#

 MailMessage mail = new MailMessage();
        mail.To.Add("syam229280@gmail.com");
        mail.To.Add("deepu04852@gmail.com");
        mail.From = new MailAddress("syam229280@gmail.com");
        mail.Subject = "Email using Gmail";

        string Body = "Hi, this mail is to test sending mail" +
                      "using Gmail in ASP.NET";
        mail.Body = Body;

        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential
             ("syam229280@gmail.com", "password");

        smtp.EnableSsl = true;
        smtp.Send(mail);