none
Error to sending mail from Windows Server 2012 and c# web app RRS feed

  • Question

  • Hello everyone, I have migrated a web application in asp.net from windows server 2008 to windows server 2012, the code of "forgot your password" worked in 2008 but now it does not send the emails, I tried with an outlook client in the server 2012 and the messages if send, but since the application does not work, this is the method that I used:

    bool SendEmail(string email, string body, string subject)
        {
            bool isSend = false;
            var email = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
            
    
            var mailMessage = new MailMessage { From = new MailAddress(email.From) };
            mailMessage.To.Add(new MailAddress(email));
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            mailMessage.IsBodyHtml = true;
    
            // Send the email
            var client = new SmtpClient();
            try
            {
                client.Send(mailMessage);
                isSend = true;
                Log = "cliente host: " + client.Host + " client port: " + client.Port + " Credenciales" + client.Credentials;
            }
            catch (Exception ex)
            {
                Log = ex.Message + " stacktrace: " + ex.StackTrace + "    cliente host: " + client.Host + " client port: " + client.Port + " Credenciales" + client.Credentials;
    
                Console.WriteLine("Exception caught in CreateTestMessage4(): {0}",
                            ex.ToString());
            }
            return isSend;
    }

    And this on webconfig

    <system.net>
        <mailSettings>
          <!-- E-mail server settings -->
          <smtp from="smtp@domain.com.co">
            <network host="smtp-server.com.co" port="25" userName="" password="" defaultCredentials="true" />
          </smtp>
        </mailSettings>
      </system.net>

    I also tried this other method but it does not work

    private bool enviarCorreo(string email, string body, string subject)
        {
            bool isSend = false;
            MailAddress from = new MailAddress("email@example.com.co");
            MailAddress to = new MailAddress(email);
            MailMessage message = new MailMessage(from, to);
            message.Subject = subject;
            message.Body = body;
            SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["email"]);
            Console.WriteLine("Sending an e-mail message to {0} by using SMTP host {1} port {2}.",
                 to.ToString(), client.Host, client.Port);
    
            try
            {
                client.Send(message);
                isSend = true;
                Log = "cliente host: " + client.Host + " client port: " + client.Port + " Credenciales" + client.Credentials;
            }
            catch (Exception ex)
            {
                Log = ex.Message + " stacktrace: " + ex.StackTrace + "    cliente host: " + client.Host +" client port: "+ client.Port + " Credenciales" + client.Credentials;
                
                Console.WriteLine("Exception caught in CreateTestMessage4(): {0}",
                            ex.ToString());
            }
    
            return isSend;
        }

    and on webconfig

    <appSettings>
      <add key="email" value="smtp-domain.com.br" />
    </appSettings>

    On localhost it's works or from the server 2008, but since the server 2012 doesn't send, this is the message that the stacktrace gives me

    Failure sending mail. stacktrace: at System.Net.Mail.SmtpClient.Send(MailMessage message) at 
    OlvidoContraseña.SendEmail(String correo, String body, String asunto) cliente host: smtp-domain.com.br client 
    port: 25 CredencialesSystem.Net.System NetworkCredential
    Thx for your help
    Wednesday, January 31, 2018 2:56 PM

All replies

  • My gut instinct is that it has nothing to do with your .NET code but rather either the configuration, the mail server or your new server you're running the code on. The failure is occurring while sending the request to the mail server.

    1. Have you confirmed that you can reach the mail server from the new machine?
    2. Have you confirmed the URL and port information is correct for the mail server?
    3. Have you tried writing a simple console app or Powershell script containing just the send mail logic to eliminate any issues with the configuration?
    4. Have you looked at any inner exception details? If so then please post them.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, January 31, 2018 3:49 PM
    Moderator
  • You might try subscribing to SendCompleted event for more specific details w/o the try/catch.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 31, 2018 4:37 PM
    Moderator