Пример #1
0
        public bool Send(MailParam p, string recipient, string url = null, string customData = null)
        {
            recipient = ConfigurationManager.AppSettings["epsilon:HookMailAddress"] ?? recipient;

            var message = new MailMessage();

            message.From = new MailAddress(p.FromEmail ?? "*****@*****.**", p.From);
            message.To.Add(recipient);

            if (!string.IsNullOrEmpty(p.Reply))
                message.ReplyToList.Add(p.Reply);

            if (!string.IsNullOrEmpty(customData))
                message.Headers.Add("Mail-CustomData", customData);

            message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            message.Subject = p.Subject;
            message.IsBodyHtml = false;
            message.Body = p.ContentText + Environment.NewLine + _resourcesTemplateService.GetRes("Res.Mailing.StdSignature", p.CultureId == 0 ? 9 : p.CultureId);

            if (!string.IsNullOrEmpty(url))
            {
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(Config.Current.MailingRootUrl.TrimEnd('/') + "/" + url.TrimStart('~', '/'));
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        var r = response.GetResponseStream();
                        if (response.StatusCode == HttpStatusCode.OK && r != null)
                        {
                            string bodyHtml = null;
                            using (var reader = new StreamReader(r))
                                bodyHtml = reader.ReadToEnd();

                            var mimeType = new ContentType("text/html");
                            var alternate = AlternateView.CreateAlternateViewFromString(bodyHtml, mimeType);
                            message.AlternateViews.Add(alternate);
                        }
                    }
                }
                catch (Exception ex) { throw new Exception("Unable to obtain mail body: " + url, ex); }
            }

            var client = new SmtpClient();

            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                _exceptionLoggerService.Log(ex);
                return false;
            }

            return true;
        }
Пример #2
0
        dynamic RecoverPassword(dynamic x)
        {
            var email = Request.Query.email;

            if (!_userRepository.EmailExists(email))
                return Response.AsJson(new { Fail = _resourcesTemplateService.GetRes("Res.Auth.RecoverPassword.EmailNotFound") });

            var t = MailingService.GetCryption().SerializeObject(new string[] { DateTime.UtcNow.ToString("O"), email });
            var link = Config.Current.MailingRootUrl.TrimEnd('/') + "/Auth/AskPassword?token=" + HttpUtility.UrlEncode(t);

            var p = new MailParam
            {
                Subject = _resourcesTemplateService.GetRes("Res.Mail.RecoverPassword.Subject"),
                ContentText = _resourcesTemplateService.GetRes("Res.Mail.RecoverPassword.ContentText", new { Link = link })
            };

            if (_mailingService.Send(p, email))
                return Response.AsJson(new { Success = _resourcesTemplateService.GetRes("Res.Auth.RecoverPassword.Sent") });

            return Response.AsJson(new { Fail = _resourcesTemplateService.GetRes("Res.Auth.RecoverPassword.SendingError") });
        }