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函式測試

沒有留言:

張貼留言