I've been working on a ASP.NET MVC website and It's been hosted. You can directly send an inquire form to a specific email in the website I'm making, It's working perfectly fine, the form is sending without a problem, until I hosted it. Whenever I try to send a form, I keep getting this error:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'G:\PleskVhosts\sellurs.com\httpdocs\App_Data\uplo ads\SOS.docx'
But when I try to run my project that is not hosted, the form is still sending without a problem. Why is that? Here's my controller:
codice:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files)
{
if (ModelState.IsValid)
{
List<string> paths = new List<string>();
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
paths.Add(path);
}
}
var message = new MailMessage();
foreach (var path in paths)
{
var fileInfo = new FileInfo(path);
var memoryStream = new MemoryStream();
using (var stream = fileInfo.OpenRead())
{
stream.CopyTo(memoryStream);
}
memoryStream.Position = 0;
string fileName = fileInfo.Name;
message.Attachments.Add(new Attachment(memoryStream, fileName));
}
//Rest of business logic here
string EncodedResponse = Request.Form["g-Recaptcha-Response"];
bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
if (IsCaptchaValid)
{
var body = "<p><b>Email From:</b> {0} ({1})</p><p><b>Subject:</b> {2} </p><p><b>Message:</b></p><p>{3}</p><p><b>Description:</b></p><p>{4}</p>";
message.To.Add(new MailAddress("***")); // replace with valid value
message.From = new MailAddress("***"); // replace with valid value
message.Subject = "(Inquire)";
message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message, model.Desc);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "***", // replace with valid value
Password = "***" // replace with valid value
};
smtp.Credentials = credential;
smtp.Host = "smtp.live.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.SendCompleted += (s, e) =>
{
//delete attached files
foreach (var path in paths)
System.IO.File.Delete(path);
};
await smtp.SendMailAsync(message);
ViewBag.Message = "Your message has been sent!";
ModelState.Clear();
return View("Index");
}
} else
{
TempData["recaptcha"] = "Please verify that you are not a robot!";
}
} return View(model);
}
I think the problem is in the pathing, but I'm not really sure how to fix it. Someone please help me. I'm just new with this kind of stuffs. Thank you in advance. And also, when the website was hosted the navigation bar becomes blue, but in the local host it's black. And the background picture is not showing in the hosted website. Pleeease help me. Thank you.