C# 发送电子邮件

网上找到的发送邮件的类,改了一点点,在此谢谢原作者的奉献。

C# 发送电子邮件
.源码:   
 public class CSendMail
    {
        private MailMessage mailMessage;
        private SmtpClient smtpClient;
        private string password;//发件人密码 

        //邮件发送后提醒
        public String Info
        {
            get;
            set;
        }
        /// <summary>  
        /// 处审核后类的实例  
        /// </summary>  
        /// <param name="To">收件人地址</param>  
        /// <param name="From">发件人地址</param>  
        /// <param name="Body">邮件正文</param>  
        /// <param name="Title">邮件的主题</param>  
        /// <param name="Password">发件人密码</param>  
        public CSendMail(string To, string From, string Body, string Title, string
                       Password)
        {
            mailMessage = new MailMessage();
            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)
            {
                try
                {
                    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.Timeout = 3000; //设置超时时间
                    smtpClient.Send(mailMessage);
                }
                catch (SmtpException ex)
                {
                      Info = "邮件发送失败"; 
                }
            }
        }

        private static void CompletedMethod(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            String token = (string)e.UserState;
            if (e.Error != null)
            {
                // 异步发送的错误信息
                // Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
            }
        }
    }

 .使用,经测试QQ/163/sina均可使用
  
   TYKY_OA.Web.CSendMail sm = new CSendMail(this.qqNum.Value.Trim() + "@qq.com", "XXX@163.com", "已重置你的的密码,新密码为:" + password + "  请注意保管!", "密码重置邮件", "××××××");
                        sm.Send();
                        //发送失败
                        if (sm.Info != null)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "tooltip", "alert(‘邮件发送失败,可能的原因是网络故障或邮箱不可达...‘)",true);
                        }
                        //发送成功
                        else
                        {
                            //修改数据库密码字段
                            staff.pwd = password;
                            if (bllstaffs.Update(staff))
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "tooltip", "alert(‘密码已重置,请注意查收邮件‘)", true);
                            }
                            else
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "tooltip", "alert(‘密码重置失败)", true);
                            }

                        }
C# 发送电子邮件

C# 发送电子邮件

上一篇:Linux 程序管理


下一篇:Linux fork之后,到底是子进程先运行还是父进程先运行【转】