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