Asked by:
error code via runing

Question
-
User-1634604574 posted
when in i click on submit i get exception at this line
string path = "~/Content/Upload/" + Guid.NewGuid() + "/";
here is example
Monday, July 27, 2020 12:28 PM
All replies
-
User1120430333 posted
string path = "~/Content/Upload/" + Guid.NewGuid().tostring() + "/";
maybe, the line should look like above.
Monday, July 27, 2020 1:11 PM -
User753101303 posted
Hi,
Or a compile time error? What if you try :
string
path =
"~/Content/Upload/"
+ Guid.NewGuid().ToString() +
"/"
;
Monday, July 27, 2020 1:12 PM -
User-1634604574 posted
when i click on submit i get break point at this line
-
string
path =
"~/Content/Upload/"
+ Guid.NewGuid().ToString() +
"/"
;
the exception is not visible
Monday, July 27, 2020 1:14 PM -
-
User-1634604574 posted
now i don't have any error and exception but the email is not send this message appear
Failure sending mail.
can you run that example on your side?i mentioned the link
Monday, July 27, 2020 1:20 PM -
User753101303 posted
Use ex.ToString() rather than ex.Message to get full details and in particuliar the base exception. More likely the problem is using the correct settings for the mail server you are using rather than really a coding issue. You are still trying with Gmail ?
Monday, July 27, 2020 1:48 PM -
User-1634604574 posted
yes i am still trying with Gmail i don't know how to settings for the mail server
here is my code
sendemail.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace SendEmail.Models { public class SendEmail { [Required] [RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$", ErrorMessage = "Invalid email")] public string to { get; set; } [Required] public string subject { get; set; } [Required] public string body { get; set; } public HttpPostedFileBase[] file { get; set; } } }
sendemail controller
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Net.Mail; using System.IO; namespace SendEmail.Controllers { public class SendEmailController : Controller { // GET: SendEmail public ActionResult Index() { return View(); } [HttpPost] [ValidateInput(false)] public ActionResult Index(SendEmail.Models.SendEmail sendEmail) { // string path = "~/Content/Upload/" + Guid.NewGuid() + "/"; string path = "~/Content/Upload/" + Guid.NewGuid().ToString() + "/"; if (ModelState.IsValid) { MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress("zhyanadil.it@gmail.com"); mailMessage.To.Add(new MailAddress(sendEmail.to)); mailMessage.CC.Add(new MailAddress("chnar.ise.1992@gmail.com")); mailMessage.Bcc.Add(new MailAddress("chnar.ise.1992@gmail.com")); mailMessage.Subject = sendEmail.subject; mailMessage.IsBodyHtml = true; mailMessage.Body = sendEmail.body; if (sendEmail.file[0] != null) { Directory.CreateDirectory(Server.MapPath(path)); foreach (HttpPostedFileBase file in sendEmail.file) { string filePath = Server.MapPath(path + file.FileName); file.SaveAs(filePath); mailMessage.Attachments.Add(new Attachment(Server.MapPath(path + file.FileName))); } } SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential("zhyanadil.it@gmail.com", "***"); client.Host = "relay-hosting.secureserver.net"; try { client.Send(mailMessage); ViewBag.Result = "Mail Sent"; } catch (Exception ex) { ViewBag.Result = ex.Message; } } return View(); } [HttpPost] public ActionResult Reset() { return RedirectToAction("Index"); } } }
index.cshtml
<style> #viewContent h3 { margin: 10px 0; } #viewContent h3 { color: #ee14f7; text-align: left; } #viewContent .emailDiv label { display: block; margin: 0; text-transform: capitalize; } #viewContent table tr td > span { display: block; color: burlywood; } #viewContent .emailDiv > form > div > div { padding-top: 25px; } #viewContent table { width: 100%; } </style> <script src="~/Content/Component/ckeditor/ckeditor.js" type="text/javascript" language="javascript"></script> @model SendEmail.Models.SendEmail @{ var title = "Send HTML Email with Attachments in ASP NET MVC using C#"; ViewBag.Title = title; <h1>@title</h1> } <h2> <a href="http://www.yogihosting.com/asp-net-send-email/">Read the tutorial on YogiHosting » </a> @using (Html.BeginForm("Reset", "SendEmail")) { <button id="reset">Reset »</button> } </h2> <div id="viewContent"> <h3>@ViewBag.Result</h3> <div class="emailDiv"> @using (Html.BeginForm("Index", "SendEmail", FormMethod.Post, new { enctype = "multipart/form-data" })) { <table> <tr> <td> @Html.LabelFor(model => model.to) @Html.EditorFor(model => model.to) @Html.ValidationMessageFor(model => model.to) </td> <td> @Html.LabelFor(model => model.subject) @Html.EditorFor(model => model.subject) @Html.ValidationMessageFor(model => model.subject) </td> </tr> <tr> <td> @Html.LabelFor(model => model.file) @Html.TextBoxFor(m => m.file, new { type = "file", multiple = "multiple" }) @Html.ValidationMessageFor(model => model.file) </td> </tr> <tr> <td colspan="2"> @Html.LabelFor(model => model.body) @Html.TextAreaFor(model => model.body) <script type="text/javascript" language="javascript"> CKEDITOR.replace(@Html.IdFor(model => model.body)); </script> @Html.ValidationMessageFor(model => model.body) </td> </tr> <tr><td><button id="submitButton" type="submit">Submit</button></td></tr> </table> } </div> </div>
i don' have any error with code
can you tell me how can i setting up my mail server?
Monday, July 27, 2020 3:21 PM -
User1120430333 posted
yes i am still trying with Gmail i don't know how to settings for the mail server
You need to be sending email on port 587.
https://www.hesk.com/knowledgebase/?article=72
Monday, July 27, 2020 3:50 PM -
User-1634604574 posted
can you tell what is wrong with my code? and where should i use 587 port? can you run my sample on your side?
Monday, July 27, 2020 3:55 PM -
User475983607 posted
can you tell what is wrong with my code? and where should i use 587 port? can you run my sample on your side?
Your code never sets the port. The community has no idea what port "relay-hosting.secureserver.net" uses. As recommend in all your recent posts on this subject.... It is up to you to read the docs. In my experience, email relays are setup by the network folks and only internal server can use the relay. If you purchased a relay service then READ THE DOCUMENTATION!!!!
SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential("zhyanadil.it@gmail.com", "***"); client.Host = "relay-hosting.secureserver.net"; client.Port = 25
Secondly, if you are trying to use your gmail login for the relay then you are totally lost.
Monday, July 27, 2020 4:56 PM -
User-1634604574 posted
also email was not sent i think i have error at this line
client.Host = "smtpout.asia.secureserver.net";
how can i find my gmail host?
please read this article https://www.yogihosting.com/asp-net-send-email/
Monday, July 27, 2020 5:59 PM -
User409696431 posted
smtp.gmail.com, port 465, SSL
so:
client.Host = "smtp.gmail.com"; client.Port = 465; client.EnableSsl = true
Wednesday, July 29, 2020 4:18 AM