电子邮件发送失败时如何抛出异常

时间:2015-05-22 05:29:58

标签: c# asp.net-mvc asp.net-mvc-4 actionmailer

我正在使用.Net MVC为一个Iphone应用程序编写Web Api,我的问题是我想在电子邮件发送失败时抛出异常,并且我使用ActionMailer.net 7.0.2发送邮件为我注册的透视服务器名称我的web.config文件。当邮件没有收到注册用户时,它应该通过Message:There is a problem in sending registeration mail, please contact admin@XXXX.com

的例外

UserController中:

[HttpPost]
    public HttpResponseMessage Signup(SignupRequest request)
    {
        if (request.User.UserName == "" || request.User.Password == "" || request.User.Email == "")
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest, new Result<int>
            {
                Status = false,
                Message = string.Format(CultureInfo.CurrentCulture, "{0} Parameter is null", "request"),
                Data = default(int)
            });
        }
        if (request.User == null)
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest, new Result<int>
            {
                Status = false,
                Message = string.Format(CultureInfo.CurrentCulture, "{0} Parameter is null", "User"),
                Data = default(int)
            });
        }

        HttpResponseMessage response;

        try
        {
            const string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                                      @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                                      @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            var re = new Regex(emailRegex);
            if (!re.IsMatch(request.User.Email))
            {
                throw new Exception("Email is not valid");
            }

            request.User.UserName = request.User.UserName;
            request.User.Email = request.User.Email;
            request.User.Password = AppGlobal.EncryptPassword(request.User.Password);
            request.User.CreatedDate = DateTime.UtcNow;
            request.User.ModifiedDate = DateTime.UtcNow;

            Result<long> userResult = _userBusiness.Signup(request.User);


            if (!userResult.Status)
                throw new Exception(userResult.Message);

            try
            {
                var registrationEmail = new SignupEmailModel
                {
                    EmailTo = request.User.Email,
                    EmailFrom = ConfigurationManager.AppSettings["SignupEmailFrom"],
                    EmailSubject = ConfigurationManager.AppSettings["SignupEmailSubject"],
                    UserId = userResult.Data,
                    ViewName = "SignupEmail",
                };

                new EmailHelper().SendSignup(registrationEmail).Deliver();


            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            response = Request.CreateResponse(HttpStatusCode.OK, userResult);
            userResult.Message = string.Format(CultureInfo.CurrentCulture, MessageStatus.Signup_User_Signup_Successfully);
        }
        catch (Exception ex)
        {
            response = Request.CreateResponse(HttpStatusCode.OK, new Result<int>
            {
                Status = false,
                Message = ex.Message,
                Data = default(int)
            });
        }

        return response;
    }

    #endregion

0 个答案:

没有答案