2016-07-12 22 views
1

Ich habe auf einer Website mit ASP.NET MVC arbeiten auf dieser Website können Sie direkt eine E-Mail an eine bestimmte E-Mail-Adresse senden. Es funktioniert einwandfrei, die E-Mail sendet ohne Probleme. Nicht bis ich es gehostet habe. Ich erhalte diesen Fehler:Senden von E-Mails funktioniert in localhost aber nicht in gehosteten Website (GoDaddy)

An attempt was made to access a socket in a way forbidden by its access permissions 65.55.176.126:587

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 65.55.176.126:587

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Ich weiß nicht, warum es auf localhost funktioniert, aber nicht in gehosteten Website. Jemand bitte hilf mir. Ich bin ein Neuling. Vielen Dank im Voraus. Hier ist mein 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(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); 

     } 
+0

Sie haben dies in einem Kommentar '// mit gültigen value' ersetzen. Haben Sie diese tatsächlich durch gültige Werte ersetzt? – Andrew

+0

@Andrew Ja Sir, ich habe einen gültigen Wert angegeben. –

+0

Bitte fügen Sie den *** Text *** der Fehlermeldung, die Sie erhalten, anstelle eines Screenshots hinzu. Es ist das wichtigste Beweisstück in deiner Frage und es ist in einem Bild versteckt. Jeder andere, der dieses Problem hat, wird diese Frage nicht finden. – spender

Antwort

0

ich denke, es der Hafen sein kann, das Sie verwenden. Sie müssen eine andere SMTP-Bibliothek verwenden (anstelle von StmpClient), wenn Ihr Port gemäß msdn über 465 liegt.

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

+0

Ich habe meinen Port geändert. Und es gibt keinen Fehler mehr, Sir. Aber wenn ich versuche, es zu senden, bleibt es einfach "verbinden" und sendet überhaupt nicht. Was vielleicht die Ursache dafür? Vielen Dank. –

+0

Es muss jeder Port sein, den Ihr SMTP-Mailserver zulässt. Wenn sie nur den in Ihrer Frage angegebenen zulassen, müssen Sie möglicherweise denselben Port beibehalten und den Code vollständig ändern – Andrew