163企业邮件的发送

 public class WriteEmail
    {
        private string UserName = "系统管理员";
        /// <summary>
        ///  POP3,SMTP     
        ///  例如:smtp.qq.com||smtp.loogtream.com 
        /// </summary>
        private string EmailHost = "smtp.163.com"; //邮件服务器SMTP
        private string UserAddress = "[email protected]"; //发件人邮箱  填写示例邮箱  不一定真实存在
        private string Password = "11111";  //发件人邮箱密码 
        private string Title = "邮件标题";
        private string FilePath = "";
        //接受邮件的人
        private string ToUser = "邮件接收者 例如:[email protected]";
        private void IsOk()
        {
            if (string.IsNullOrEmpty(UserAddress))
            {
                throw new Exception("未输入:");
            }
        }
        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="content">邮件内容</param>
        public void Send(string content)
        {
            try
            {
                IsOk();
                SmtpClient smtp = new SmtpClient();
                smtp.Host = EmailHost;
                smtp.Credentials = new NetworkCredential(UserAddress, Password); //邮件服务器验证信息
                smtp.EnableSsl = true;
                MailMessage myMail = new MailMessage();
                myMail.From = new MailAddress(UserAddress, UserName);
                var toaddresss = ToUser.Split(',');
                foreach (var item in toaddresss)
                {
                    myMail.To.Add(new MailAddress(item));
                }
                if (!string.IsNullOrEmpty(FilePath))
                    myMail.Attachments.Add(new Attachment(FilePath));
                myMail.Subject = Title;
                myMail.SubjectEncoding = Encoding.UTF8;
                myMail.Body = content;
                myMail.BodyEncoding = Encoding.UTF8;
                myMail.IsBodyHtml = true;
                try
                {
                    smtp.Send(myMail);
                }
                catch (Exception)
                {

                    throw;
                }

            }
            catch (Exception)
            {
                throw;
            }
        }


    }

猜你喜欢

转载自blog.csdn.net/cy520ta/article/details/81331057
今日推荐