使用GMAIL寄信
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class sendGmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("寄件者帳號@gmail.com", "寄件者顯示名稱"); //
msg.To.Add(new MailAddress("收件人的MAIL帳號@livemail.tw", "收件者顯示名稱")); //可寫迴圈多筆寄送
msg.Subject="主旨";
msg.IsBodyHtml=true;
string str ="<h2>內文1</h2>";
str += "<h3>內文2</h3>";
str += "<h3>543</h3>"; // html上的東西都能收,圖檔要注意路徑,http://網址/資料夾/檔名,也能放超連結
msg.Body = str;
// msg.Attachments //附件
// msg.CC //副本
SmtpClient clinet= new SmtpClient("smtp.gmail.com");
////可用asp.net組態,管理工具設定,下面可以不用自己寫,但要手動去 Web.config 加上 enableSsl = true;
NetworkCredential loginInfo = new NetworkCredential("gmail帳號@gmail.com", "gmail帳號的密碼");
clinet.UseDefaultCredentials = false;
clinet.Credentials = loginInfo;
clinet.EnableSsl = true;
//// clinet.Send("寄件者帳號@gmail.com", "收件者帳號@livemail.tw", "主旨", "內文"); //單筆方式
clinet.Send(msg);
}
}
////Web.config會加上這段,自己加上 enableSsl = "true"
<!--
</connectionStrings><system.net>
<mailSettings>
<smtp from="寄件者的顯示@gmail.com">
<network host="smtp.gmail.com" password="gamil密碼" userName="gmail帳號" enableSsl = "true"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
-->
測試文字功能,小工具,標題
記錄很重要,不然會浪費很多時間在找以前的記憶
一個人的氣度,決定他未來的高度。
2013年7月12日 星期五
2013年7月9日 星期二
Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class M4_linqEnt : System.Web.UI.Page
{
NorthwindModel.NorthwindEntities northwindEntitie = new NorthwindModel.NorthwindEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
show();
}
}
protected void show()
{
var _shippers = from s in northwindEntitie.Shippers
select s;
foreach (var s in _shippers)
{
Response.Write(s.ShipperID + " , " + s.CompanyName + " , "+s.Phone + "<br>");
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
NorthwindModel.Shipper _shipper = new NorthwindModel.Shipper(); //新增,要建立一個新的
_shipper.CompanyName = "123123";
_shipper.Phone = "0202020202";
northwindEntitie.Shippers.AddObject(_shipper); //新增add
northwindEntitie.SaveChanges();
show();
}
protected void btnup_Click(object sender, EventArgs e)
{
int txtb1 = int.Parse(TextBox1.Text);
NorthwindModel.Shipper _shipper = (from s in northwindEntitie.Shippers //修改,要先查詢出資料
where s.ShipperID == txtb1
select s).Single();
_shipper.CompanyName = "111111";
_shipper.Phone = "0202-0202";
northwindEntitie.SaveChanges(); //修改直接存檔
show();
}
protected void btndel_Click(object sender, EventArgs e)
{
int txtb1 = int.Parse(TextBox1.Text);
NorthwindModel.Shipper _shipper = (from s in northwindEntitie.Shippers // 刪除也要先查出資料
where s.ShipperID == txtb1
select s).Single();
northwindEntitie.Shippers.DeleteObject(_shipper); // 刪除DeleteObject
northwindEntitie.SaveChanges(); //存檔
show();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class M4_linqEnt : System.Web.UI.Page
{
NorthwindModel.NorthwindEntities northwindEntitie = new NorthwindModel.NorthwindEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
show();
}
}
protected void show()
{
var _shippers = from s in northwindEntitie.Shippers
select s;
foreach (var s in _shippers)
{
Response.Write(s.ShipperID + " , " + s.CompanyName + " , "+s.Phone + "<br>");
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
NorthwindModel.Shipper _shipper = new NorthwindModel.Shipper(); //新增,要建立一個新的
_shipper.CompanyName = "123123";
_shipper.Phone = "0202020202";
northwindEntitie.Shippers.AddObject(_shipper); //新增add
northwindEntitie.SaveChanges();
show();
}
protected void btnup_Click(object sender, EventArgs e)
{
int txtb1 = int.Parse(TextBox1.Text);
NorthwindModel.Shipper _shipper = (from s in northwindEntitie.Shippers //修改,要先查詢出資料
where s.ShipperID == txtb1
select s).Single();
_shipper.CompanyName = "111111";
_shipper.Phone = "0202-0202";
northwindEntitie.SaveChanges(); //修改直接存檔
show();
}
protected void btndel_Click(object sender, EventArgs e)
{
int txtb1 = int.Parse(TextBox1.Text);
NorthwindModel.Shipper _shipper = (from s in northwindEntitie.Shippers // 刪除也要先查出資料
where s.ShipperID == txtb1
select s).Single();
northwindEntitie.Shippers.DeleteObject(_shipper); // 刪除DeleteObject
northwindEntitie.SaveChanges(); //存檔
show();
}
}
DataReader
private DataTable RunSqlString()
{
using (SqlConnection conn = new SqlConnection(textBox1.Text))
{
DataTable dt = null;
String strCmd = textBox2.Text;// "Select md_date,md_room from meeting_date where md_date=@md_date and md_room=@md_room ";
using (SqlCommand cmd = new SqlCommand(strCmd, conn))
{
//cmd.Parameters.AddWithValue("@md_date", strdate);
try
{
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
dt = new DataTable("tmp");
for (int i = 0; i < dr.FieldCount; i++)
{
dt.Columns.Add(dr.GetName(i));
}
while (dr.Read())
{
DataRow drw = dt.NewRow();
for (int i = 0; i < dr.FieldCount; i++)
{
drw[i] = dr[i].ToString();
}
dt.Rows.Add(drw);
}
}
else
{
MessageBox.Show("沒有資料!!!");
}
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
throw;
} //要先開啟連線
finally
{
conn.Close();
}
}
return dt;
}
}//結尾
=======================================================================
protected bool check_meetingroom_pk(string strdate ,string strroom ) {
bool b = false;
//步驟一給予適當的名稱空間namespace using System.Data.SqlClient;
//步驟二建立連線物件
String strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
using (SqlConnection conn = new SqlConnection(strConn))
{
//步驟三建立Command物件讀取資料庫的資料
String strCmd = "Select md_date,md_room from meeting_date where md_date=@md_date and md_room=@md_room ";
using (SqlCommand cmd = new SqlCommand(strCmd, conn))
{
cmd.Parameters.AddWithValue("@md_date", strdate);
cmd.Parameters.AddWithValue("@md_room", strroom);
//步驟四建立DataReader物件處理讀出來的資料
//透過Command物件所提供的ExecuteReader的方法來產生Command
//要先開啟連線
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
//步驟五在瀏覽器上顯示資料
//讀取資料之前要先呼叫read()的方法
while (dr.Read())
{
b = true; // dr[0].ToString();
}
conn.Close();
}
}
return b;
}
DataAdapter
String strConn = "data source=.;user id=aa;password=aa;initial Catalog=northwind";
using (SqlConnection conn = new SqlConnection(strConn))
{
String strCmd = "Select ProductID,ProductName from Products where categoryID=@categoryID";
using (SqlDataAdapter da = new SqlDataAdapter(strCmd, conn))
{
da.SelectCommand.Parameters.AddWithValue("@categoryID", 1);
DataSet ds = new DataSet();
da.Fill(ds, "dsProducts");
////新增,刪除,修改才要用
//SqlCommandBuilder builder = new SqlCommandBuilder(da);
////新增
//builder.GetInsertCommand();
//DataTable productsTable = ds.Tables["dsProducts"];//修改會用這這行
//DataRow newRow = productsTable.NewRow();
//newRow["ProductName"] = "IP5S";
//newRow["ProductID"] = "1";
//productsTable.Rows.Add(newRow);
////修改
//builder.GetUpdateCommand();
//productsTable.Rows[1]["ProductName"] = "KEVIN";
////刪除
//builder.GetDeleteCommand();
//ds.Tables["dsProducts"].Rows[12].Delete();
////最後更新
//da.Update(ds, "dsProducts"); //最後更新
//查詢用到的迴圈
foreach (DataRow theRow in ds.Tables["dsProducts"].Rows)
{
Response.Write(theRow[0].ToString() + "," + theRow["ProductName"].ToString() + "<br>");
}
訂閱:
文章 (Atom)