Saturday 15 August 2015

asp.net mvc - Sending email MVC4 C# -


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

View:

 using  & lt;% (HTMLsegmentform ("Sedlink", "Home"))% & gt; & Lt;% {% & gt; & Lt; Input type = "text" id = "toemail" value = "" /> & Lt; Input type = "submit" value = "send" /> & Lt;%}% & gt;   

Controller:

  Public action Sendlink () {Return view (); } [AcceptVerbs] Public Action Result Collect (formulate form clause) {try {string message = session ["link"]. ToString (); String toemail = formCollection ["toemail"]; MailEngine.Send ("mail@mail.com", toemail, "link", message); Return Redirect Reaction ("CanvasShare"); } Hold {} return tap; }   

Class MailEngine:

  Send Public Static Zero (String, String To, String Subject, String Body) {try {MailMessage mail = new MailMessage } (From, to, subject, body); SmtpClient Client = New SmtpClient ("smtp.mymail.com"); Customer Distribution method = SmtpDeliveryMethod.Network; Client.EnableSsl = false; Client.Send (mail); } Hold {}}    

toemail will always be empty .

You need to set the input's name attribute: "toemail" to make it bindable

  & lt; Input type = "text" id = "toemail" name = "toemail" />   

Occasionally, as others say, it's not really a good idea to use empty cat statements. It hides a potential bug like your case There is a hidden exception in the try hold block, resulting in empty screen results, thus showing a blank screen.

There are many options, how to handle exceptions in ASP MVC. My favorite one exception filter and & lt; CustomArser's mode = "on" /> gt; Web.config is a combination of settings.

  Protected Override Zero on Express (expansion filter filter filter) {base.OnException (filterContext); If (filterContext.HttpContext.IsCustomErrorEnabled) {if (filterContext.Exception is security Exception) {filterContext.ExceptionHandled = true; FilterContext.Result = View ("FriendlyError"); // to log exceptions etc ...}}}   

So when a custom error is enabled, you can return a friendly error screen on the output, or You can disable it to see the actual exception while debugging.

No comments:

Post a Comment