How to throw exception when email sending failed

I'm writing Web Api for a Iphone app using .Net MVC and my issue is that i want to throw Exception when email sending failed and i m using 7.0.2 for sending mails for perspective server name i registered in my web.config file. When mail does not gets to received to the registered user it should through a exception that Message:There is a problem in sending registeration mail, please contact [email protected]


[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

Category:c# Time:2018-08-11 Views:0

Related post

  • Netty. Server throws exceptions when sends data to all clients 2015-02-11

    Yo. I want to create simple server. When client connects - its Channel added to list of Channels. And when server receiving message from some client, message is sent to all clients. If there is one client and server sends message to that client - all

  • Elmah throws exception when accessing Context.Request.ServerVariables 2010-09-22

    I'm using Elmah to log exceptions for my website, everything seems to be all working fine until one day I noticed that the 500 server errors are not being caught properly. I'm using following script to specifically ignore errors from ScriptResource.a

  • TcpClient.BeginRead/TcpClient.EndRead doesn't throw exception when internet disconnected 2011-02-01

    I'm using TcpListener to accept & read from TcpClient. The problem is that when reading from a TcpClient, TcpClient.BeginRead / TcpClient.EndRead doesn't throw exception when the internet is disconnected. It throws exception only if client's proc

  • Throw exception when file not found, in mathematica 2011-02-27

    I have a function that processes an image whose path is given by the user. I'm a bit new to Mathematica, and I can't find much on its large documentation. How can I throw an exception when Import[myFile] fails? Can I even do that? Thanks a lot. -----

  • nHibernate does not throw exceptions when expected (invoking Save on entity in a collection)? 2011-04-26

    I am just getting started using nHibernate, and some behavior is a bit disturbing, which is it seemingly does not throw exceptions when you try to perform some action when you have either coded it oncorrectly, or have a configuration error. For examp

  • JAX-WS: How to handle exceptions when client aborted 2012-01-30

    JAX-WS: How to handle exceptions when client aborted Hi. Could you please tell me how to handle exceptions in JAX-WS? The following exception was occured when the client closed connection suddenly. I'd like to log the exceptions. How can I catch the

  • Hibernate doesn't throw an exception when DB connection fails 2011-12-22

    I am using hibernate in a java project to do mapping to oracle database. I was trying to handle exception when give to access to database. But hibernate seems giving no exception, just stuck there forever. Could anyone give me some suggestion? How do

  • How to handle exception when Directory.GetFiles() throws an exception when it finds a file name it does not "like"? 2009-05-27

    On a Vista machine with the valid path C:\Users\David, calling Directory.GetFiles(@"C:\Users\David") throws the following ArgumentException when run as the David user, who can view the contents of the directory just fine in Windows Explorer: System.A

  • python unittest assertRaises throws exception when assertRaises fails 2009-08-04

    I've got code where assertRaises throws an exception when assertRaises fails. I thought that if assertRaises fails then the test would fail and I'd get a report at the end that says the test failed. I wasn't expecting the exception to be thrown. Belo

  • Throw exception when access image from App.xaml.cs 2011-12-30

    First, try this. <Window x:Class="WpfApplication1.MainWindow" xmlns="" xmlns:x="" Height="80" Width="100" Loaded="Window_Loaded"> <Image x:N

  • Exception when client sends message to server 2011-12-31

    I am using netty 3.2.7. When I send a message (contained in DAOMessage) from the client to the server on the same computer it works correctly. If I do exactly the same but have the client and server on different computers I get the following exceptio

  • Outlook - email sending fails repeatedly 2013-03-01

    I have Outlook 2010, with links set up to accounts at Google Apps and Microsoft Exchange. I receive emails fine, and send them fine most of the time. However, sometimes the programme simply refuses to send particular emails, and I can't work out why.

  • Is it a good or bad idea throwing Exceptions when validating data? 2009-10-01

    When validating data, I've gotten into a habit of doing the following *Note: i dont really have individual booleans for each check. This is just for the example. *Another Note: any error handling during the tests are done properly. The ONLY exception

  • z:Id = "" in WCF Message log. DataContractSerializer throws exception when Deserializing 2010-12-10

    I'm trying to deserialize messages from a captured WCF Message log (svclog) using the DataContractSerializer. Some objects throw an exception when try to call dataContractSerializer.ReadObject() The exception thrown is: "Invalid Id ''. Must not be nu

  • How to prevent builds when another build fails? 2011-02-10

    I have created many build configurations in Hudson for a single solution (eg. Release, Debug, Test) When I commit something wrong, I receive 1 build failed e-mail for every build configuration. I would like to receive a single e-mail. I think if I co

  • how to display toast when email or mms is received to the other person 2011-03-07

    I am sending emails and mms in my project. I want to show a tost when email or mms is received to the recipient. I am using built-in email and messaging application. I don't know how to implement this please help. thanx --------------Solutions-------

  • VBScript throw exception when the receiving object is C# COM object and the function has interface as parameter 2011-05-31

    Here is my VBScript code Set object= CreateObject("Demo. Object") Set service= CreateObject("Demo.ManageServer ") service.SetObject object I get the following message: Error: Invalid procedure call or argument: ‘Service.SetObject’ Code: 800A005 Demo.

  • jTDS JDBC not throwing exception when it should 2011-12-11

    I have a problem with the following code using the jTDS JDBC Driver. Everything works, and queries is no problem. But I don`t get an error/exception if the connection is failing. I have tried to enter a false IP, disable local network connection, pro

  • Email Sending Failed 2011-12-23

    I am developing an application for sending emails. I have used the EDMESSAGE framework. I developed the application in MAC OS X 10.6.7 and i got it working. When i installed the application in MAC OS X 10.7.2, email sending is failed. The error that

Copyright (C), All Rights Reserved.

processed in 2.199 (s). 13 q(s)