- 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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。