Sending Email MVC4 C #

I am trying to send an email inside an action. However, the action always returns a blank screen.

View:

<% using(Html.BeginForm("Sendlink", "Home")) %> <% { %> <input type="text" id="toemail" value="" /> <input type="submit" value="Send" /> <% } %> 

Controller:

 public ActionResult Sendlink() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Sendlink(FormCollection formCollection) { try { string message = Session["link"].ToString(); string toemail = formCollection["toemail"]; MailEngine.Send(" mail@mail.com ", toemail, "link", message); return RedirectToAction("CanvasShare"); } catch { } return null; } 

MailEngine Class:

 public static void Send(string from, string to, string subject, string body) { try { MailMessage mail = new MailMessage(from, to, subject, body); SmtpClient client = new SmtpClient("smtp.mymail.com"); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = false; client.Send(mail); } catch { } } 
+4
source share
2 answers

toemail will always be null.

You need to set the name attribute for input: "toemail" to make it related.

 <input type="text" id="toemail" name="toemail" /> 

Never, as others say, it makes no sense to use empty statements. This hides a potential error. As in your case, there is a hidden exception in the try catch , which leads to the result of a null action, so a blank screen appeared.

There are several options for handling exceptions in ASP MVC. My favorite is a combination of an exception filter and settings <CustomErrors mode="On"/> web.config.

 protected override void OnException(ExceptionContext filterContext) { base.OnException(filterContext); if (filterContext.HttpContext.IsCustomErrorEnabled) { if (filterContext.Exception is SecurityException) { filterContext.ExceptionHandled = true; filterContext.Result = View("FriendlyError"); //log the exception etc... } } } 

Therefore, when a custom error is enabled, you can return a friendly production error screen or disable it to see the actual exception during debugging.

+2
source

Your application uses empty catch blocks. This is not a good idea, for a more detailed discussion of this issue, see Why do empty catch blocks block a bad idea? and related issues.

It seems that the following is happening to you:

  • An exception is Sendlink(FormCollection formCollection) somewhere inside the try block of your Sendlink(FormCollection formCollection) method Sendlink(FormCollection formCollection) . This exception seems to occur in a call to RedirectToAction("CanvasShare") , since all other calls inside this try block do not throw an exception. (Especially since you suppressed the exceptions thrown by the MailEngine.Send method.)
  • An empty catch block is Sendlink(FormCollection formCollection) in Sendlink(FormCollection formCollection) . This is the place where you should create an error message and display it to the user. However, you decided to leave it empty so that no one would know that something went wrong and that it was.
  • The control thread reaches the return null; in your Sendlink(FormCollection formCollection) method Sendlink(FormCollection formCollection) . I assume you put this there because the compiler complained about the missing return value. This null is now returned and results in a visualization of the empty view.

The obvious solution is to check RedirectToAction and find out why it throws an exception. This exception probably indicates a problem in your code or application, and you need to take steps to prevent it from occurring.

The next fix is ​​to actually implement error handling in your application. Remove all empty catch blocks and think about whether you want to throw an exception or if you want to handle it right then and there. Ignoring this, however, is almost never a good idea.

To illustrate the problem in your application: if an exception is not thrown in RedirectToAction , your email may still fail. However, your user interface has no way to find out that something went wrong because you are ignoring the exception in the MailClient.Send method. If you ever send this to production, emailing will not work quietly and your customers will be wondering why they never received emails. Then it will be difficult for you to find out what the actual problem is and where it occurred.

+3
source

Source: https://habr.com/ru/post/1487415/


All Articles