Upload
truong-tien
View
893
Download
1
Embed Size (px)
Citation preview
Các hàm hay s d ng trong ASP.NET C#ử ụHàm G i mail ra các mail server ngoài nh Gmail hay yahoo mail,…ở ư
Using 2 th vi n tr c khi s d ng hàm này:ư ệ ướ ử ụ
using System.Net.Mail;using System.Net;
/// <summary> /// hàm gởi mail đi. /// </summary> /// <param name="TieuDe"></param> /// <param name="NoiDung"></param> /// <param name="diachimail"></param> /// <param name="goitudiachi"></param> /// <returns></returns> public string GoiMail(string TieuDe, string NoiDung,string diachimail,string goitudiachi) { try { MailMessage mailMsg = new MailMessage(); mailMsg.From = new MailAddress(goitudiachi, "Ban Quản Trị Website Bán Mắt Kính."); mailMsg.To.Add(diachimail); mailMsg.Subject = TieuDe; mailMsg.IsBodyHtml = true; mailMsg.Body = NoiDung; mailMsg.Priority = MailPriority.Normal; //phuong thuc dung de gui mail tuong ung voi dia chi may chu SmtpClient client = new SmtpClient("smtp.gmail.com", 587);//smtp.gmai.com 587 ,465
//khi reply lai se vao dia chi nay(dia chi nay lam trung gian de gui toi dia chi txtto.text NetworkCredential info = new NetworkCredential("[email protected]", "luudanpartner");//[email protected] client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = info; client.EnableSsl = true; client.Send(mailMsg); return "Gởi đi thành công."; } catch (Exception ex) { return "Gởi không thành công."; } }
Hàm t o m ng băm, dành cho ch c năng đăng ký thành viên và đăng nh p, mã hóa m t kh u:ạ ả ứ ậ ậ ẩ
Using th vi n tr c khi s d ng hàm này:ư ệ ươ ử ụ
using System.Text;using System.Security.Cryptography;
public string TaoMangBam(string chuoicanbam) { string kq = ""; HashAlgorithm Bam = HashAlgorithm.Create("MD5"); byte[] pwordData = Encoding.Default.GetBytes(chuoicanbam); //tao ma bam byte[] hash = Bam.ComputeHash(pwordData); //hien thi ma bam // Response.Write(BitConverter.ToString(hash)); kq = BitConverter.ToString(hash); return kq; }
Hi n th yahoo h tr tr c tuy n:ể ị ỗ ợ ự ế
<a href="ymsgr:sendim?<%# Eval("TenNick") %>&m=Xin Chào"><a href='ymsgr:sendim?<%# Eval("TenNick") %>&m=Xin Chào'>
<img alt="Nhắp vào đây để liên hệ với admin" border="0px" height="40" src='http://opi.yahoo.com/online?u=<%# Eval("TenNick") %>∓m=g&t=14=us' width="70" /></a> </a>
Hàm phát sinh mã t đ ng:ự ộ
/// <summary> /// Hàm phát sinh mã tự động cho các table /// </summary> /// <param name="strID">Chuỗi nhận dạnd. Ví du : AGB000000001 thì chuỗi nhận dạng là AGB</param> /// <param name="field">Field mã số cần phát sinh</param> /// <param name="table">Table cần phát sinh mã</param> /// <param name="length">Chiều dài của mã số cần phát sinh</param> /// <returns>Chuỗi mã số</returns> protected string CreateIDCode(string strID, string field, string table, int length) { try { this.Connect(); string IDCode = strID + "0000000000"; string query = "select max(" + field.Trim() + ") from " + table.Trim() + " where left(" + field.Trim() + "," + strID.Length + ")='" + strID.Trim() + "'";
this.command = new OleDbCommand(query, this.connection); this.command.CommandType = CommandType.Text; OleDbDataReader reader = this.command.ExecuteReader();
while (reader.Read()) if (!reader.IsDBNull(0)) IDCode = Convert.ToString(reader.GetValue(0));
IDCode = IDCode.Substring(strID.Length);
IDCode = "0000000000000000" + Convert.ToString(Convert.ToInt64(IDCode) + 1); IDCode = strID + IDCode.Substring(IDCode.Length - length + strID.Length);
this.Disconnect(); return IDCode; } catch (Exception ex) { this.Disconnect(); throw ex; } }
Hàm chuy n đ i đ nh d ng ngày gi :ể ổ ị ạ ờ
public string ChuyenDoiNgayGioVietNam(DateTime time) { return time.Day.ToString()+"/"+time.Month.ToString()+"/"+time.Year.ToString(); }
public string ChuyenDinhDangVNSangCSDLA(string ngay)// 19/05/2011 { if (ngay.Length < 9) { ngay = "0" + ngay; ngay = ngay.Substring(0, 3) + "0" + ngay.Substring(3, 6); return ngay.Substring(3, 2) + "/" + ngay.Substring(0, 2) + "/" + ngay.Substring(6, 4); } if (ngay.Length < 10)//02/5/2011---4 { int i = ngay.IndexOf("/"); if (i < 2) { ngay = "0" + ngay; } else { ngay = ngay.Substring(0, 3) + "0" + ngay.Substring(3, 6); } return ngay.Substring(3, 2) + "/" + ngay.Substring(0, 2) + "/" + ngay.Substring(6, 4); }
else { return ngay.Substring(3, 2) + "/" + ngay.Substring(0, 2) + "/" + ngay.Substring(6, 4); } }
public string ChuanHoaNgayThang(string ngay,ref bool dungdinhdang)// 19/05/2011 { string ngayhoanchinh = ""; try { if (ngay.Length < 9 && ngay.Length >= 8) { ngay = "0" + ngay; ngay = ngay.Substring(0, 3) + "0" + ngay.Substring(3, 6); ngayhoanchinh = ngay.Substring(3, 2) + "/" + ngay.Substring(0, 2) + "/" + ngay.Substring(6, 4);
} else if (ngay.Length < 10 && ngay.Length >= 8)//02/5/2011---4 { int i = ngay.IndexOf("/"); if (i < 2) { ngay = "0" + ngay; } else { ngay = ngay.Substring(0, 3) + "0" + ngay.Substring(3, 6); } ngayhoanchinh = ngay.Substring(3, 2) + "/" + ngay.Substring(0, 2) + "/" + ngay.Substring(6, 4);
} else { ngayhoanchinh = ngay.Substring(3, 2) + "/" + ngay.Substring(0, 2) + "/" + ngay.Substring(6, 4);
}
DateTime ngay_ = DateTime.Parse(ngayhoanchinh); dungdinhdang = true; } catch { ngayhoanchinh = ""; dungdinhdang = false; }
return ngayhoanchinh; }