Search This Blog

Saturday, October 30, 2010

E-Mail Send - Resevie C# Code

using System;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address@mfc.ae");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
 
 
Send Email in C# Code
  
// create mail message object
MailMessage mail = new MailMessage();
mail.From = "";           // put the from address here
mail.To = "";             // put to address here
mail.Subject = "";        // put subject here 
mail.Body = "";           // put body of email here
SmtpMail.SmtpServer = ""; // put smtp server you will use here 
// and then send the mail
SmtpMail.Send(mail);
==================================================
String server = "127.0.0.1";
 
private void btnSend_Click(object sender, System.EventArgs e)
{
 MailAddress to = new MailAddress("admin@.mail.adr");
 MailAddress from = new MailAddress(txtFrom.Text);
 MailMessage message = new MailMessage(from, to);  
 
 message.Subject = txtSubject.Text;
 message.Body = @"" txtContent.Value;

 SmtpClient client = new SmtpClient(server);
 
 try
 {
 client.Send(message);
  }
 catch (Exception ex)
 {
 throw new Exception(ex.Message, ex);
 }
}
 
 
 
Other Example 
using System;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace TestingConsole
{
  class Program
  {
      static void Main(string[] args)
      {
          try
          {
              string to = "to@domain.com";
              string from = "from@gmail.com";
              string from_pwd = "mypassword";
              string subject = "Sample Mail testing";
              string body = "Wow this is testing body";
              MailMessage mM = new MailMessage();
              mM.From = new MailAddress(from);
              mM.To.Add(to);
              mM.Subject = subject;
              mM.Body = body;
              mM.IsBodyHtml = false;
              mM.Priority = MailPriority.High;
              SmtpClient sC = new SmtpClient("smtp.gmail.com");
              sC.Port = 587;
              sC.Credentials = new NetworkCredential(from,from_pwd);
              sC.EnableSsl = true;
              sC.Send(mM);
          }
          catch (Exception e)
          {
              Console.WriteLine(e.Message + " " + e.StackTrace);
          }
      }
  }
}
 

Out Of Date Change UserName and Password

PasswordExpires, Part I 
 
public class PasswordExpires
{
  DomainPolicy policy;
  
  const int UF_DONT_EXPIRE_PASSWD = 0x10000;
 
  public PasswordExpires()
  {
    //get our current domain policy
    Domain domain = Domain.GetCurrentDomain();
    DirectoryEntry root = domain.GetDirectoryEntry();
  
    using (domain)
    using (root)
    {
      this.policy = new DomainPolicy(root);
    }
  }
 
 PasswordExpires, Part II
 
 
public DateTime GetExpiration(DirectoryEntry user)
  {
    int flags = 
      (int)user.Properties["userAccountControl"][0];
 
    //check to see if password is set to expire
    if(Convert.ToBoolean(flags & UF_DONT_EXPIRE_PASSWD))
    {
      //the user’s password will never expire
      return DateTime.MaxValue;
    }
 
    long ticks = GetInt64(user, "pwdLastSet");
 
    //user must change password at next login
    if (ticks == 0)
      return DateTime.MinValue;
 
    //password has never been set
    if (ticks == -1)
    {
      throw new InvalidOperationException(
        "User does not have a password"
        );
    }
 
    //get when the user last set their password;
    DateTime pwdLastSet = DateTime.FromFileTime(
      ticks
      );
 
    //use our policy class to determine when
    //it will expire
    return pwdLastSet.Add(
      this.policy.MaxPasswordAge
      );
  } 
 
 
 PasswordExpires, Part III 

public TimeSpan GetTimeLeft(DirectoryEntry user)
  {
    DateTime willExpire = GetExpiration(user);
 
    if (willExpire == DateTime.MaxValue)
      return TimeSpan.MaxValue;
 
    if (willExpire == DateTime.MinValue)
      return TimeSpan.MinValue;
 
    if (willExpire.CompareTo(DateTime.Now) > 0)
    {
      //the password has not expired
      //(pwdLast + MaxPwdAge)- Now = Time Left
      return willExpire.Subtract(DateTime.Now);
    }
 
    //the password has already expired
    return TimeSpan.MinValue;
  }
  
  private Int64 GetInt64(DirectoryEntry entry, string attr)
  {
    //we will use the marshaling behavior of
    //the searcher
    DirectorySearcher ds = new DirectorySearcher(
      entry,
      String.Format("({0}=*)", attr),
      new string[] { attr },
      SearchScope.Base
      );
      
    SearchResult sr = ds.FindOne();
    
    if (sr != null)
    {
      if (sr.Properties.Contains(attr))
      {
        return (Int64)sr.Properties[attr][0];
      }
    }
    return -1;
  }


Checking Password Expiration


string adsPath = "LDAP://CN=User1,OU=Users,DC=domain,DC=com";
 
DirectoryEntry user = new DirectoryEntry(
  adsPath,
  null,
  null,
  AuthenticationTypes.Secure
  );
 
string attrib = "msDS-User-Account-Control-Computed";
 
using (user)
{
  user.RefreshCache(new string[] { attrib });
 
  int flags = (int)user.Properties[attrib].Value
    & (int)AdsUserFlags.PasswordExpired);
 
  if (Convert.ToBoolean(flags)
  {
    //password has expired
    Console.WriteLine("Expired");
  }
}





 
 

Login Password Case-Sensitive

 private void btnlogin_Click(object sender, EventArgs e)
        {
            login();
        }
        private void ClearFields()
        {
            txtusername.Text = String.Empty;
            txtpwd.Text = String.Empty;
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void txtpwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                login();
            }
        }
        private void login()
        {
            try
            {
                ConnectionClass l_ConnectionClass = new ConnectionClass();
                DataSet l_UserDataSet = l_ConnectionClass.FetchDataInDataSet("select * from tbluserlogin where userName = '" + txtusername.Text.ToLower().Trim() + "' and userPwd = '" + txtpwd.Text.Trim() + "'");
                if (l_UserDataSet.Tables[0].Rows.Count > 0)
                {
                    PublicVariables.CheckLoginFlag = true;
                    PublicVariables.OSUserID = Convert.ToInt16(l_UserDataSet.Tables[0].Rows[0]["userId"]);
                    PublicVariables.UserRole = l_UserDataSet.Tables[0].Rows[0]["userRole"].ToString();
                    DateTime getdatefrm = l_ConnectionClass.getlogouttime();
                    DateTime datenow = DateTime.Now;
                    if ((getdatefrm.AddMinutes(0) > datenow) && (getdatefrm != DateTime.MinValue))
                    {
                        MessageBox.Show("please wait");
                        ClearFields();
                        PublicVariables.CheckLoginFlag = false;                  
                    }
                    else
                    {
                        l_ConnectionClass.updateuserlogin();
                        Close();
                        this.Hide();
                    }
                }
                else
                {
                    MessageBox.Show("Login Failed !!!!", "Error", MessageBoxButtons.OK);
                    PublicVariables.CheckLoginFlag = false;
                    txtusername.Text = "";
                    txtpwd.Text = "";
                    txtusername.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection Failed. Plz Check your Network Cable Connection", "Error", MessageBoxButtons.OK);
            }
        }
        private void txtusername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                txtpwd.Focus();
            }
        }

Thursday, October 14, 2010

Sql Connection String Examples

A data connection includes a connection string that is typically provided by the owner of the external data source. The following table lists examples of connections strings for different types of external data sources.
Data sourceExampleDescription
SQL Server database on the local serverdata source="(local)";initial catalog=AdventureWorksSet data source type to SQL Server.
SQL Server instance databaseData Source=localhost\MSSQL10_50.InstanceName; Initial Catalog= AdventureWorksSet data source type to SQL Server.
SQL Server Express databaseData Source=localhost\MSSQL10_50.SQLEXPRESS; Initial Catalog= AdventureWorksSet data source type to SQL Server.
Analysis Services database on the local serverdata source=localhost;initial catalog=Adventure Works DWSet data source type to SQL Server Analysis Services.
SharePoint Listdata source=http://MySharePointWeb/MySharePointSite/Set data source type to SharePoint List.
SQL Server 2000 Analysis Services serverprovider=MSOLAP.2;data source=;initial catalog=FoodMart 2000Set the data source type to OLE DB Provider for OLAP Services 8.0.
You can achieve a faster connection to SQL Server 2000 Analysis Services data sources if you set the ConnectTo property to 8.0. To set this property, use the Connection Properties dialog box, Advanced Properties tab.
Report ModelsNot applicable. You do not need a connection string for a report model. In Report Builder, browse to the report server and select the .smdl file that is the report model.
Oracle serverdata source=myserverSet the data source type to Oracle. The Oracle client tools must be installed on the Report Builder computer and on the report server.
SAP NetWeaver BI data sourceDataSource=http://mySAPNetWeaverBIServer:8000/sap/bw/xml/soap/xmlaSet the data source type to SAP NetWeaver BI.
Hyperion Essbase data sourceData Source=http://localhost:13080/aps/XMLA; Initial Catalog=SampleSet the data source type to Hyperion Essbase.
Teradata data sourcedata source=...; Set the data source type to Teradata. The connection string is an Internet Protocol (IP) address in the form of four fields, where each field can be from one to three digits.
Teradata data sourceDatabase=; data source=N>...NN>;Use X Views=False;Restrict to Default Database=TrueSet the data source type to Teradata, similar to the previous example. Only use the default database that is specified in the Database tag, and do not automatically discover data relationships.
XML data source, Web servicedata source=http://adventure-works.com/results.aspxSet the data source type to XML. The connection string is a URL for a web service that supports Web Services Definition Language (WSDL).
XML data source, XML documenthttp://localhost/XML/Customers.xmlSet the data source type to XML. The connection string is a URL to the XML document.
XML data source, embedded XML documentEmptySet the data source type to XML. The XML data is embedded in the report definition.