发送电子邮件可在localhost中运行,但不能在托管网站(GoDaddy)

时间:2016-07-12 10:12:12

标签: c# asp.net-mvc hosting filepath email-attachments

我一直在使用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);

        }

1 个答案:

答案 0 :(得分:0)

我认为它可能是您正在使用的端口。根据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