您好,登錄后才能下訂單哦!
- using System;
- using System.Net;
- using System.Net.Mail;
- using System.Net.Mime;
- namespace Fish
- {
- /**/
- /// <summary>
- /// 發送郵件的類
- /// </summary>
- public class SendMail
- {
- private MailMessage mailMessage = new MailMessage();
- private SmtpClient smtpClient;
- private string password;//發件人密碼
- /**/
- /// <summary>
- /// 設置收件人、發件人信息與郵件內容、標題
- /// </summary>
- /// <param name="To">收件人地址</param>
- /// <param name="From">發件人地址</param>
- /// <param name="Body">郵件正文</param>
- /// <param name="Title">郵件的主題</param>
- /// <param name="Password">發件人密碼</param>
- public void Mail(string To, string From, string Body, string Title, string Password)
- {
- mailMessage.To.Add(To);
- mailMessage.From = new System.Net.Mail.MailAddress(From);
- mailMessage.Subject = Title;
- mailMessage.Body = Body;
- mailMessage.IsBodyHtml = true;
- mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
- mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
- this.password = Password;
- }
- /**/
- /// <summary>
- /// 添加附件
- /// </summary>
- public void Attachments(string Path)
- {
- string[] path = Path.Split(',');
- Attachment data;
- ContentDisposition disposition;
- for (int i = 0; i < path.Length; i++)
- {
- data = new Attachment(path[i], MediaTypeNames.Application.Octet);//實例化 附件
- disposition = data.ContentDisposition;
- disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//獲取 附件的創建日期
- disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);// 獲取附件的修改日期
- disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//獲取附 件的讀取日期
- mailMessage.Attachments.Add(data);//添加到附件中
- }
- }
- /**/
- /// <summary>
- /// 異步發送郵件
- /// </summary>
- /// <param name="CompletedMethod"></param>
- public void SendAsync(SendCompletedEventHandler CompletedMethod)
- {
- if (mailMessage != null)
- {
- smtpClient = new SmtpClient();
- smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//設置發件人身份的票據
- smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
- smtpClient.Host = "smtp." + mailMessage.From.Host;
- smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注冊異步發送郵件完成時的事件
- smtpClient.SendAsync(mailMessage, mailMessage.Body);
- }
- }
- /**/
- /// <summary>
- /// 發送郵件
- /// </summary>
- public void Send()
- {
- if (mailMessage != null)
- {
- smtpClient = new SmtpClient();
- smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//設置發件人身份的票據
- smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
- smtpClient.Host = "smtp." + mailMessage.From.Host;
- smtpClient.Send(mailMessage);
- }
- }
- /// <summary>
- /// 根據索引刪除附件
- /// </summary>
- /// <param name="Id"></param>
- public void DeleteAttachments(int Id)
- {
- mailMessage.Attachments.RemoveAt(Id);
- }
- }
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。