2012年3月26日 星期一

[C#] IP,Ping

CMD 下的 Ping 工具一直是很簡便的 網路連線偵測工具,本範例是使用

Net.framework 2 的 Ping 類別 來達成目的。

 

專案下載

KM-000

private void button1_Click(object sender, EventArgs e)
        {
            Ping p1 = new Ping();
            p1.PingCompleted += new PingCompletedEventHandler(this.PingCompletedCallBack);
            if (textBox1.Text.Trim().Length == 0)
            {
                return;
            }
            p1.SendAsync(textBox1.Text.Trim(), null);
        }

private void PingCompletedCallBack(object sender, PingCompletedEventArgs e)
        {
            listBox1.Items.Clear();

            if (e.Cancelled)
            {
                listBox1.Items.Add("Ping Canncel");
                return;
            }

            if (e.Error != null)
            {
                listBox1.Items.Add("e.Error.Message");
                return;
            }

            PingReply reply = e.Reply;
            listBox1.Items.Add(reply.Address + " " + reply.Status.ToString());
            if (reply.Status == IPStatus.Success)
            {
                listBox1.Items.Add("RoundTrip time:" + reply.RoundtripTime);
                listBox1.Items.Add("Time to live:"+reply.Options.Ttl);
                listBox1.Items.Add("Don't fragment:"+reply.Options.DontFragment);
                listBox1.Items.Add("Buffer size:"+reply.Buffer.Length);
            }
        }

2012年3月6日 星期二

[C#] MailMessage,SmtpClient,寄信,E-Mail


使用 smtpClient 送信

//引用
using System.Net.Mail;
using System.Net.Mime;

//主要方法
 public void send_email(string msg, string mysubject, string address)
        {
            MailMessage message = new MailMessage(test@123.co.jp, address);//MailMessage(寄信者, 收信者)
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;//E-mail編碼
            message.Subject = mysubject;//E-mail主旨
            message.Priority = MailPriority.Normal; //優先權
            message.Body = msg;//E-mail內容
            string file_name = @"C:\Users\km\Desktop\fdkbc.xls"; //要寄送的添付檔案
            Attachment data = new Attachment(file_name, MediaTypeNames.Application.Octet);
            ContentDisposition disposition = data.ContentDisposition;
            disposition.CreationDate = System.IO.File.GetCreationTime(file_name);
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file_name);
            disposition.ReadDate = System.IO.File.GetLastAccessTime(file_name);
            message.Attachments.Add(data);
            SmtpClient smtpClient = new SmtpClient("172.23.120.254", 25);//設定E-mail Server和port
            smtpClient.Send(message);
        }

//使用方法
 send_email("測試內容", "測試主旨標題", "mailto:kuomingwang@gmail.com%22);//呼叫send_email函式測試