常用的方式集

STMP邮件发送   生成随机数   Serv-U6.0.0.2加减解算法   图片缩放   Base64之编码,解码方法

//Response.Write(SendMail("主题","[email protected]", "[email protected]" ,"正文了!")); //调用

public string SendMail(
string subject, string form, string to, string body)
{

  //主题 发 收 内容

   
string
Tform, Tto, MailErr;
   //比较域名是否相同 -- 收发人域名相同是呼发送不能成功!

   Tform = Regex.Replace(form,".*@","");
   Tto  = Regex.Replace(to ,".*@","");

   if (Tform.ToLower() != Tto.ToLower())
   {
      MailErr = "发送成功!";
      try {
         try {
            MailMessage myMail = new MailMessage();
            myMail.BodyFormat = MailFormat.Html;
            myMail.Subject = subject; //主题
            
myMail.From = form; //发件人的 Email
            
myMail.To = to; //收件人 Email
            //MyMessage.Cc = body; //抄送副本
            myMail.Body = body; //正文内容
            try {
               //SmtpMail.SmtpServer= "localhost"; //设置Eamil发送服务器,不设置默认为本机
               SmtpMail.Send(myMail); //发送
            
}
            catch(System.Web.HttpException ehttp)
            {
               MailErr = "<a style='color:#FF0000;font-size:12px;'>"+ehttp.Message+"下面是完整的错误信息:";
               MailErr += "</A><br><hr><br><a style='font-size:12px;'>"+ehttp.ToString()+"</A>";
            }
         }
         catch(IndexOutOfRangeException) {
               MailErr = "<a style='color:#FF0000;font-size:12px;'>错误:Email地址不正确!</a>";
         }
      }
      catch(System.Exception e) {
         MailErr = "<a style='color:#FF0000;font-size:12px;'>"+e.Message+"下面是完整的错误信息:";
         MailErr += "</A><br><hr><br><a style='font-size:12px;'>"+e.ToString()+"</A>";
      }
   }
   else
      MailErr = "收发件人的域名不能相同,换个非'"+Tform+"'的试试!";
   return MailErr;
}


//生成随机数
public string MakePassword()
{
  int pwdlen = 2; //生成随机字符的位数
  string pwdchars = "abcdefghijklmnopkrstuvwxyzZBCDEFGHIJKLMNOPKRSTUVWXYZ"; //生成的字符包含那字
  string tmpstr = "";
  int iRandNum;
  Random rnd = new Random();
  for(int i=0;i<pwdlen;i++)
  {
    iRandNum = rnd.Next(pwdchars.Length);
    tmpstr += pwdchars[iRandNum];
  }
  return tmpstr;
}


 //Serv-U6.0.0.2加减解算法

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Security" %>

<script language="C#" runat="server">
public void yanzheng(object sender, System.EventArgs e)
{
string A = open(UserName.Text, UserPassword.Text);
string B;
if (A == "1") Response.Write("无此账号"+UserName.Text);
else
{
B = JiaMi(A, UserPassword.Text);
if (A == B) Response.Write("OK!密码是:<br>"+B);
else Response.Write("不正确:"+B+"<br>正确密码是:"+A);
}

}
public void New(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("Server=(local);Integrated Security=SSPI;database=Serv-U");
SqlCommand cmd = new SqlCommand("UPDATE [useraccounts] SET password=@password where name = @name", conn);

cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar, 40));
cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 40));

cmd.Parameters["@name"].Value = UserName.Text;
cmd.Parameters["@password"].Value = NewMiMa(NewUserPassword.Text);

cmd.Connection.Open();
cmd.ExecuteNonQuery();
Response.Write("修改成功");
cmd.Connection.Close();

}
public string open(string UserName, string UserPassword) //检查有无此账号,有返回1并赋给password原密码的值
{
SqlConnection conn = new SqlConnection("Server=(local);Integrated Security=SSPI;database=Serv-U");
SqlCommand cmd = new SqlCommand("select top 1 [name],[password] from [useraccounts] where name = '"+ UserName +"'", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
UserPassword = dr["password"].ToString();
conn.Close();
return UserPassword;
}
else
{
conn.Close();
return "1";
}
}
public string JiaMi(string UserPassword, string Password) //取原密码前两位来加密对对
{
string Password2 = UserPassword.Substring(0,2);
Password = Password2 + Password; //取密码的左边两位
Password = FormsAuthentication.HashPasswordForStoringInConfigFile(Password,"MD5");
return Password2 + Password;
}
public string NewMiMa(string Password) //新密码加密
{
string a = MakePassword();
string p = a + Password;
Password = FormsAuthentication.HashPasswordForStoringInConfigFile(p,"MD5");
return a + Password;
}
public string MakePassword()
{
int pwdlen = 2; //生成随机字符的位数
string pwdchars = "abcdefghijklmnopkrstuvwxyzZBCDEFGHIJKLMNOPKRSTUVWXYZ"; //生成的字符包含那字
string tmpstr = "";
int iRandNum;
Random rnd = new Random();
for(int i=0;i<pwdlen;i++)
{
iRandNum = rnd.Next(pwdchars.Length);
tmpstr += pwdchars[iRandNum];
}
return tmpstr;
}
</script>
<Form runat="server">
账号:<asp:TextBox ID="UserName" runat="server" /><br>
密码:<asp:TextBox ID="UserPassword" runat="server" />
<asp:Button ID="TiJiao" Text="提交" runat="server" OnClick="yanzheng" />
<br><br>

新密码:<asp:TextBox ID="NewUserPassword" runat="server" />
<asp:Button ID="s" Text="提交" runat="server" OnClick="New" />
</Form>


// 图片缩放处理
private void img()
{
System.Drawing.Image oldimg = System.Drawing.Image.FromFile(@"f:\a.jpg");
System.Drawing.Image newimg = oldimg.GetThumbnailImage( 60, 60 * oldimg.Height/oldimg.Width, null, new IntPtr(0));
newimg.Save(@"f:\new.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
oldimg.Dispose();
newimg.Dispose();
}


// Base64 编码
编码
public string EncodeBase64(string code_type,string code)
{
string encode = "";
byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
try
{
encode = Convert.ToBase64String(bytes);
}
catch
{
encode = code;
}
return encode;
}
解码
public string DecodeBase64(string code_type,string code)
{
string decode = "";
byte[] bytes = Convert.FromBase64String(code);
try
{
decode = Encoding.GetEncoding(code_type).GetString(bytes);
}
catch
{
decode = code;
}
return

你可能感兴趣的:(常用)