2

I'm having a Send Mail function on my site. When debugging on localhost, everything works fine. But when site is published to the web, the SendMail-function returns this error message in the browser everytime i try to send a mail: "Your request timed out. Please retry the request."

Why does it work on localhost, but not on the live published website? What can be the issue?

Here is my code in my mvc-controller:

[HttpPost]
    public ActionResult SendMail(HomeContentPage model)
    {
        const string username = "myMailAdress@gmail.com";
        const string password = "mySecretPassword";
        var loginInfo = new NetworkCredential(username, password);
        var msg = new MailMessage();

        var smtpClient = new SmtpClient("smtp.gmail.com", 587)
        {
            EnableSsl = true,
            UseDefaultCredentials = false,
            Credentials = loginInfo
        };

        string message = model.SendMail.SenderName + " have a message for you:<br /><br /><br />" + model.SendMail.Message
                         + "<br /><br />Avs. Phone: " + model.SendMail.Phone
                         +"<br /><br />Avs. Mail: " + model.SendMail.SenderAddress;
        var page = RavenSession.Load<ContentPage>(model.Id) as HomeContentPage;
        try
        {
            msg.From = new MailAddress("dwfff@hello.com", "MySite.com");
            msg.To.Add(new MailAddress("myMailAdress@gmail.com"));
            msg.Subject = "MySite.com";
            msg.Body = message;
            msg.IsBodyHtml = true;

            smtpClient.Send(msg);

            page.SendMail.IsSuccess = true;
            page.Url = page.Url + "#contact";

            return View(page.Template.Name, page);
        }
        catch (Exception)
        {
            return View(page.Template.Name, page);
        }
    }
user3228992
  • 1,373
  • 1
  • 16
  • 31
  • where is the site being hosted..? also are you by chance storing any settings in a .config file..? do you have a system admin – MethodMan Sep 22 '14 at 18:55
  • @DJKRAZE The site is hosted at: https://www.citynetwork.se/ . I don't think I'm storing any mail-settings in a configfile, what type of settings would that be? //Thanks – user3228992 Sep 22 '14 at 18:58
  • does the hosted site support .NET..? – MethodMan Sep 22 '14 at 18:59
  • @DJKRAZE yes it supports .NET. The site is online. It's just the SendMail-function that doesn't work when site is online – user3228992 Sep 22 '14 at 19:00
  • ok out of curiosity can you show the using statement I wonder if you have a dependency issue – MethodMan Sep 22 '14 at 19:11
  • this should help you out .. http://www.c-sharpcorner.com/UploadFile/sourabh_mishra1/sending-an-e-mail-using-Asp-Net-mvc/ – MethodMan Sep 22 '14 at 19:12
  • Your outgoing SMTP connections may be firewalled out. Have you checked it? – AnFi Sep 22 '14 at 19:12
  • @AndrzejA.Filip How do I check that? – user3228992 Sep 22 '14 at 19:13
  • @DJKRAZE `using System.Net.Mail;` if that's what you mean? – user3228992 Sep 22 '14 at 19:17
  • is this MVC app or standard .NET app can you add some logging perhaps since you can't debug this remotely I take it .. – MethodMan Sep 22 '14 at 19:18
  • Do you have access to windows/unix shell at hosting server? My experience is based on "full admin (root)" perspective. – AnFi Sep 22 '14 at 19:21
  • here is another working example the only difference I see is how they use MailMessage Object and the To and From Email it differs from yours http://stackoverflow.com/questions/18988348/error-in-sending-email-via-a-smtp-client – MethodMan Sep 22 '14 at 19:22
  • could there also be an issue with var loginInfo = new NetworkCredential(username, password); did you try connecting manually to the email – MethodMan Sep 22 '14 at 19:23
  • also what about this `smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;` – MethodMan Sep 22 '14 at 19:25
  • I'm going to try out your proposed examples. I think it will take a while until these parts of my site will be republished and working online so I can test it // thanks – user3228992 Sep 22 '14 at 19:31
  • @DJKRAZE By add some logging. Do you mean the for example: the network tab in my chrome dev-tool whn I try to send a message? I'm getting a respone that the statusCode for sending is OK. But the message is never sent to the mail. It also takes almost 2 minutes waiting in the timing – user3228992 Sep 22 '14 at 20:10
  • no in code meaning in places where you thing errors may occur then start removing by process of elimination until you pinpoint the exact line of code or method call that of which is causing the error – MethodMan Sep 22 '14 at 20:41
  • @DJKRAZE I don't know how to do that. There are no errors as far as I can see. Where should I look for them? – user3228992 Sep 22 '14 at 20:51
  • you mean to tell me that you do not know how to add simple logging to your code..? how about adding some additional logging in the catch block and trying to capture the InnerText in regards to the Exception message..? – MethodMan Sep 22 '14 at 20:53
  • @DJKRAZE sorry I totally misunderstood what you were saying. I will try that – user3228992 Sep 22 '14 at 20:55
  • no problem . your logging can be anything from logging an entry to the `EventLog, Sql Server, TextFile, Console, etc...` – MethodMan Sep 22 '14 at 20:56

0 Answers0