0

I have following code which works fine if I host the website in IIS of my local system. But if I host the website on my Windows Server, it doesn't send mail.

Code of Controller:

[HttpPost]
public ActionResult Index()
{
    string tomail = "test@godaddyserver.com";
    string SMTPServer = "smtpout.secureserver.net";
    string SMTPUserName = "test@godaddyserver.com";
    string SMTPPassword = "test123456";
    string strMailTitle = "Inquiry Mail";
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient(SMTPServer);
    SmtpServer.Credentials = new NetworkCredential(SMTPUserName, SMTPPassword);
    mail.From = new MailAddress(tomail, strMailTitle);
    SmtpServer.Port = 25;
    mail.To.Add(tomail);
    mail.Subject = "Inquiry Mail";
    mail.IsBodyHtml = true;

    string mailTemplatePath = Server.MapPath("~/template/AdminContactUs.html");
    StringBuilder MyStringBuilder = new StringBuilder();

    if (System.IO.File.Exists(mailTemplatePath))
        MyStringBuilder.Append(System.IO.File.ReadAllText(mailTemplatePath));

    mail.Body = MyStringBuilder.ToString();
    try
    {
        SmtpServer.ServicePoint.MaxIdleTime = 1;
        SmtpServer.Send(mail);
    }
    catch (Exception e)
    {

    }
}

One thing I forgot to add is that it is working fine with Exchange Server email(test@mydomain.com) and SMTP(smtp1.mydomain.com). Current email and SMTP is of Godaddy. However, it is not working with Godaddy, Hostgator or Gmail.

Can anybody please suggest me if I am missing anything?

Nanji Mange
  • 2,155
  • 4
  • 29
  • 63
  • 1
    What error you get? – Divyang Desai Aug 26 '16 at 06:42
  • @div I am not getting any error in my development system. It is working fine and send email from my local system. I am not able to debut from Windows Server as it doesn't have VS and I also not set any mechanism to show error from Server. – Nanji Mange Aug 26 '16 at 06:44
  • In your `catch (Exception e)` block, try to write the exception message and stack trace to a text file, then tell us the exception message and stack trace. Refer to [this answer](http://stackoverflow.com/a/1268773/1905949) for the code to write to text file. – ekad Aug 26 '16 at 07:02
  • @ekad let me try as you suggest. – Nanji Mange Aug 26 '16 at 07:04

0 Answers0