【问题标题】:Sending email works in localhost but not in hosted website(GoDaddy)发送电子邮件在 localhost 中有效,但在托管网站中无效(GoDaddy)
【发布时间】: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


【解决方案1】:

我认为这可能是您使用的端口。如果您的端口根据 msdn 高于 465,则需要使用不同的 SMTP 库(而不是 StmpClient)。

https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

【讨论】:

  • 我改变了我的端口。先生,没有更多的错误。但是当我尝试发送它时,它只是继续“连接”,根本不发送。可能是什么原因造成的?谢谢。
  • 它需要是您的 smtp 邮件服务器允许的任何端口。如果他们只允许您问题中给出的那个,那么您可能需要保持相同的端口并完全更改代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多