Search code examples
c#emailvisual-studio-2015smtpsmtpclient

Email sending code working on localhost but not on server


I have written an email sending code using c#. I used GoDaddy's server for my website. It is working on localhost (VS 2013), But when deployed the same code on the server it gives the following error:


Server Error in '/' Application.

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond [2607:f8b0:400e:c05::6c]:25

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: A connection attempt fail because the connected party did not properly respond after a period of time, or establish connection fail because connected host has failed to respond [2607:f8b0:400e:c05::6c]:25

Stack Trace:

[SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed t SMS.Portal.SendSMS.btnSubmit_Click(Object sender, EventArgs e) in d:\Paul\ProximityMarketer\smsproximitysource\source\Portal\SendSMS.aspx.cs:30 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9633514 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 System.Web.UI.WebControls.Button.System.Web...IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

Version Information: Microsoft .NET Framework version:4.0.30319; ASP.NET version:4.0.30319.34280


Code is given below:

SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = "smtp.gmail.com";
smtpclient.Port = 25;
smtpclient.Credentials = new NetworkCredential("[email protected]", "Password");
smtpclient.EnableSsl = true;
MailMessage objmsg = new MailMessage();
objmsg.From = new MailAddress("[email protected]");
objmsg.To.Add(email);
objmsg.Subject = "testing";
objmsg.Body = myString.ToString();
objmsg.IsBodyHtml = true;
smtpclient.Send(objmsg);

Solution

  • I found the solution. Someone recommended me the Article. https://pk.godaddy.com/help/see-your-smtp-relays-3552. And I used the following code:

    SmtpClient ss = new SmtpClient();
    ss.Host = "smtpout.secureserver.net";
    ss.Port = 80;
    ss.Timeout = 10000;
    ss.DeliveryMethod = SmtpDeliveryMethod.Network;
    ss.UseDefaultCredentials = false;
    ss.EnableSsl = false;
    ss.Credentials = new NetworkCredential("[email protected]", "password");
    
    MailMessage mailMsg = new MailMessage("[email protected]", email, "subject here", "my body");
    mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    ss.Send(mailMsg);
    

    Note: It was not working with port 25.