Ejemplo n.º 1
0
        public ActionResult SaveSetting(ChemCloud.Model.ChemCloud_EmailSetting model)
        {
            if (!base.ModelState.IsValid)
            {
                Result result = new Result()
                {
                    success = false,
                    msg     = "数据验证错误!"
                };
                return(Json(result));
            }

            Result result1 = new Result();

            if (model.Id > 0)
            {
                if (ServiceHelper.Create <IEmailSettingService>().EditEmailSetting(model))
                {
                    result1.success = true;
                    result1.msg     = "编辑成功!";
                }
                else
                {
                    result1.success = false;
                    result1.msg     = "编辑失败!";
                }
            }
            else
            {
                if (ServiceHelper.Create <IEmailSettingService>().AddEmailSetting(model))
                {
                    result1.success = true;
                    result1.msg     = "添加成功!";
                }
                else
                {
                    result1.success = false;
                    result1.msg     = "添加失败!";
                }
            }
            return(Json(result1));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 发送邮件函数 by 薛正根 2015-12-01
        /// </summary>
        /// <param name="mailTo">要发送的邮箱 多个邮箱请用";"隔开</param>
        /// <param name="mailSubject">邮箱主题</param>
        /// <param name="mailContent">邮箱内容</param>
        /// <returns>返回发送邮箱的结果</returns>
        public static bool SendEmail(string mailTo, string mailSubject, string mailContent)
        {
            var smtpServer       = "smtp.qq.com";
            var sendMailId       = "*****@*****.**";
            var sendMailPassword = "******";

            EmailSettingService _EmailSettingService = new EmailSettingService();

            ChemCloud.Model.ChemCloud_EmailSetting model = _EmailSettingService.GetChemCloud_EmailSetting();

            if (model != null)
            {
                if (!string.IsNullOrWhiteSpace(model.SmtpServer) && !string.IsNullOrWhiteSpace(model.SendMailId) && !string.IsNullOrWhiteSpace(model.SendMailPassword))
                {
                    smtpServer       = model.SmtpServer;
                    sendMailId       = model.SendMailId;
                    sendMailPassword = model.SendMailPassword;
                }
            }

            // 邮件服务设置
            SmtpClient smtpClient = new SmtpClient();

            smtpClient.DeliveryMethod        = SmtpDeliveryMethod.Network; //指定电子邮件发送方式
            smtpClient.Host                  = smtpServer;                 //指定SMTP服务器
            smtpClient.EnableSsl             = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials           = new NetworkCredential(sendMailId, sendMailPassword);//用户名和密码
            #region 发送邮件设置
            //发送给多个邮件
            if (mailTo.Contains(";"))
            {
                string[] mailToId = mailTo.Split(';');
                for (int k = 0; k < mailToId.Length; k++)
                {
                    if (mailToId[k].Trim() == "")
                    {
                        break;
                    }
                    MailMessage mailMessage = new MailMessage(sendMailId, mailToId[k].Trim()); // 发送人和收件人
                    mailMessage.Subject      = mailSubject;                                    //邮件主题
                    mailMessage.Body         = mailContent;                                    //邮件正文
                    mailMessage.BodyEncoding = Encoding.UTF8;                                  //正文编码格式
                    mailMessage.IsBodyHtml   = true;                                           //设置为HTML格式
                    mailMessage.Priority     = MailPriority.Low;                               //邮件的优先级
                    try
                    {
                        smtpClient.Send(mailMessage); // 发送邮件
                        continue;
                    }
                    catch (SmtpException ex)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                //单个邮件发送
                MailMessage mailMessage = new MailMessage(sendMailId, mailTo); // 发送人和收件人
                mailMessage.Subject      = mailSubject;                        //邮件主题
                mailMessage.Body         = mailContent;                        //邮件正文
                mailMessage.BodyEncoding = Encoding.UTF8;                      //正文编码格式
                mailMessage.IsBodyHtml   = true;                               //设置为HTML格式
                mailMessage.Priority     = MailPriority.Low;                   //邮件的优先级

                try
                {
                    smtpClient.Send(mailMessage); // 发送邮件
                    return(true);
                }
                catch (SmtpException ex)
                {
                    return(false);
                }
            }
            #endregion
        }
Ejemplo n.º 3
0
 // GET: Admin/EmailSetting
 public ActionResult Index()
 {
     ChemCloud.Model.ChemCloud_EmailSetting model = new ChemCloud.Model.ChemCloud_EmailSetting();
     model = ServiceHelper.Create <IEmailSettingService>().GetChemCloud_EmailSetting();
     return(View(model));
 }