using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Net.Mail; namespace Ajax.發郵件 { public class GetMail { //MailAddress ds = new MailAddress("1738819932@qq.com"); // Send(ds, "674580075@qq.com", "郵件主題", "郵件內容","附件名稱"); public GetMail() { } /// <summary> /// 發送電子郵件 /// </summary> /// <param name="MessageFrom">發件人郵箱地址</param> /// <param name="MessageTo">收件人郵箱地址</param> /// <param name="MessageSubject">郵件主題</param> /// <param name="MessageBody">郵件內容</param> /// <param name="SUpFile">附件</param> /// <returns></returns> public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody, string SUpFile) { MailMessage message = new MailMessage(); message.From = MessageFrom; message.To.Add(MessageTo); //收件人郵箱地址可以是多個以實現群發 message.Subject = MessageSubject; message.Body = MessageBody; if (SUpFile != "") { SUpFile = Server.MapPath("~/發郵件/Upfile/" + SUpFile);//獲得附件在本地地址 //將文件進行轉換成Attachments Attachment data = new Attachment(SUpFile, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(SUpFile); disposition.ModificationDate = System.IO.File.GetLastWriteTime(SUpFile); disposition.ReadDate = System.IO.File.GetLastAccessTime(SUpFile); message.Attachments.Add(data); System.Net.Mime.ContentType ctype=new System.Net.Mime.ContentType (); } message.IsBodyHtml = true; //是否為html格式 message.Priority = MailPriority.Normal; //發送郵件的優先等級 SmtpClient sc = new SmtpClient(); sc.Host = "smtp.qq.com"; //指定發送郵件的服務器地址或IP sc.Port = 25; //指定發送郵件端口 sc.Credentials = new System.Net.NetworkCredential("發送郵箱賬號", "賬號密碼"); //指定登錄服務器的try { sc.Send(message); //發送郵件 } catch { return false; } return true; } } }