Beispiel #1
0
        public static (bool, string) SendMailUse(Model.MailModel mail)
        {
            MailMessage msg = new MailMessage();

            foreach (string item in mail.To)
            {
                msg.To.Add(item);
            }
            msg.From            = new MailAddress(mail.MailAddress, mail.Name, Encoding.UTF8);
            msg.Subject         = mail.Subject;  //邮件标题
            msg.SubjectEncoding = Encoding.UTF8; //邮件标题编码
            msg.Body            = mail.Body;     //邮件内容
            msg.BodyEncoding    = Encoding.UTF8; //邮件内容编码
            msg.IsBodyHtml      = false;         //是否是HTML邮件
            msg.Priority        = mail.Priority; //邮件优先级

            SmtpClient client = new SmtpClient();

            client.SendCompleted += (ss, ee) => {
                if (ee.Error is SmtpFailedRecipientException smtpex)
                {
                    RGCommon.Main.ViewModel.Error = smtpex.Message;
                }
            };
            client.Credentials = new System.Net.NetworkCredential(mail.MailAddress, mail.Password);
            //注册的邮箱和密码
            client.Host = mail.SMTPHost;
            object userState = msg;

            try
            {
                client.SendAsync(msg, userState);
                return(true, "发送成功!");
            }
            catch (SmtpException ex)
            {
                return(false, ex.Message);
            }
        }
Beispiel #2
0
 private void Run()
 {
     while (true)
     {
         if (this.ViewModel.IsIntervalSend)
         {
             string result = this.ViewModel.Val();
             if (!string.IsNullOrWhiteSpace(result))
             {
                 this.ViewModel.Error = result;
                 Thread.Sleep(this.ViewModel.IntervalTime * 1000);
                 continue;
             }
             Model.MailModel mailModel = new Model.MailModel()
             {
                 To          = this.ViewModel.To,
                 Subject     = this.ViewModel.Subject,
                 Body        = this.ViewModel.Body + Environment.NewLine + "发送时间:" + DateTime.Now,
                 SMTPHost    = this.ViewModel.SMTPHost,
                 MailAddress = this.ViewModel.MailAddress,
                 Name        = this.ViewModel.Name,
                 Password    = this.ViewModel.Password,
                 Priority    = this.ViewModel.Priority,
             };
             try
             {
                 (bool state, string msg) = MailUtil.SendMailUse(mailModel);
                 this.ViewModel.Error     = msg;
             }
             catch (Exception ex)
             {
                 this.ViewModel.Error = ex.Message;
             }
             Thread.Sleep(this.ViewModel.IntervalTime * 1000);
         }
         Thread.Sleep(this.ViewModel.IntervalTime * 1000);
     }
 }
Beispiel #3
0
        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            string result = this.ViewModel.Val();

            if (!string.IsNullOrWhiteSpace(result))
            {
                RGCommon.MsgInfo(result);
                return;
            }
            Model.MailModel mailModel = new Model.MailModel()
            {
                To          = this.ViewModel.To,
                Subject     = this.ViewModel.Subject,
                Body        = this.ViewModel.Body.Replace("${DateTime}", DateTime.Now.ToString()),
                SMTPHost    = this.ViewModel.SMTPHost,
                MailAddress = this.ViewModel.MailAddress,
                Name        = this.ViewModel.Name,
                Password    = this.ViewModel.Password,
                Priority    = this.ViewModel.Priority,
            };
            (bool state, string msg) = MailUtil.SendMailUse(mailModel);
            RGCommon.MsgInfo(msg);
        }