【发布时间】:2016-11-14 12:27:52
【问题描述】:
我一直在使用 ASP.NET MVC 在这个网站上开发一个网站,您可以直接向特定的电子邮件地址发送电子邮件。它工作正常,电子邮件发送没有问题。直到我主持了它。我不断收到此错误:
试图以一种被其禁止的方式访问套接字 访问权限 65.55.176.126:587
描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。
异常详细信息:System.Net.Sockets.SocketException:尝试是 以访问权限禁止的方式访问套接字 65.55.176.126:587
来源错误:
在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
我不知道为什么它可以在 localhost 上运行,但在托管网站上却不行。有人请帮助我。我是新手。先感谢您。这是我的控制器:
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(System.Web.HttpContext.Current.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>Software 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 for SELLING)";
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);
}
【问题讨论】:
-
您在评论
// replace with valid value中有这个。你真的用有效值替换了那些吗? -
@Andrew 是的,先生,我在其中输入了一个有效值。
-
请您添加您收到的错误消息的文本,而不是屏幕截图。这是您问题中最重要的证据,它隐藏在图像中。遇到此问题的其他任何人都不会找到此问题。
-
@spender 我只是编辑我的问题先生。
标签: c# asp.net-mvc hosting filepath email-attachments