Failure sending mail.

Apex6554 60 Reputation points
         string from, pass, messageBody;
            Random rand = new Random();
            randomCode = (rand.Next(999999)).ToString();
            MailMessage message = new MailMessage();
            to = (txtEmail.Text).ToString();
            from = "*********";
            pass = "***********";
            messageBody = "your reset code is" + randomCode;
            message.From = new MailAddress(from);
            message.Body = messageBody;
            message.Subject = "Password resetting code";
            SmtpClient smtp = new SmtpClient("");
            smtp.EnableSsl = true;
            smtp.Port = 465;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(from, pass);
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,934 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,414 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 25,206 Reputation points

    Hi @Apex6554,

    It seems that your question is not related to MS SQL Server. Please remove that tag from your question.

    If your question is related to c#, please try it like below.

    All static data is stored in the app.config file.

    void Main()
          string smtpAddress = AppSettings.Get<string>("smtpAddress"); //ConfigurationManager.AppSettings["smtpAddress"];
          int portNumber = AppSettings.Get<int>("portNumber"); //ConfigurationManager.AppSettings["portNumber"];
          bool enableSSL = AppSettings.Get<bool>("enableSSL"); ;
          bool UseDefaultCredentials = AppSettings.Get<bool>("UseDefaultCredentials"); ;
          string emailFrom = ConfigurationManager.AppSettings["emailFrom"];
          string password = ConfigurationManager.AppSettings["password"];
          string emailTo = ConfigurationManager.AppSettings["emailTo"];
          string subject = ConfigurationManager.AppSettings["subject"];
          string body = ConfigurationManager.AppSettings["body"];
          StringBuilder sb = new StringBuilder();
          sb.AppendLine(string.Format("SMTP server: {0}, port: {1}", smtpAddress, portNumber));
          sb.AppendLine(string.Format("Sent from {0} machine on: {1}.", Environment.MachineName, DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz")));
          sb.AppendLine(string.Format(".Net version: {0}", Environment.Version));
          sb.AppendLine(string.Format("OS version: {0}", Environment.OSVersion.VersionString));
          body = sb.ToString();
          Console.WriteLine("SMTP server: {0}, port: {1}", smtpAddress, portNumber);
          using (MailMessage mail = new MailMessage())
             mail.From = new MailAddress(emailFrom);
             mail.Subject = subject;
             mail.Body = body;
             mail.IsBodyHtml = false;
             mail.Priority = MailPriority.High;
             // mail.Attachments.Add(new Attachment(@"C:\SomeFile.txt"));
             // mail.Attachments.Add(new Attachment(@"C:\"));
             using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                if (UseDefaultCredentials)
                   smtp.UseDefaultCredentials = true;
                   //smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
                   smtp.Credentials = new NetworkCredential(emailFrom, password);
                smtp.EnableSsl = enableSSL;
          Console.WriteLine("An e-mail has been successfully sent to: {0}.", emailTo);
       catch (Exception ex)
    // Define other methods and classes here
    public static class AppSettings
       public static T Get<T>(string key)
          var appSetting = ConfigurationManager.AppSettings[key];
          if (string.IsNullOrWhiteSpace(appSetting)) throw new SettingsPropertyNotFoundException(key);
          var converter = TypeDescriptor.GetConverter(typeof(T));
          return (T)(converter.ConvertFromInvariantString(appSetting));
          <add key="smtpAddress" value=""/>
          <add key="portNumber" value="587"/>
          <add key="enableSSL" value="true"/>
          <add key="emailFrom" value=""/>
          <add key="password" value="Password"/>
          <add key="UseDefaultCredentials" value="false"/>
          <add key="emailTo" value=""/>
          <add key="subject" value="Hello"/>
          <add key="body" value="This is e-mail testing via SMTP protocol."/>
    0 comments No comments

  2. Minxin Yu 10,441 Reputation points Microsoft Vendor

    Hi, Apex6554
    I got a Failure sending mail message when using port 465.
    Tested port 587 and used 2 step auth to gererate windows password. It worked for me.

    smtp.Port = 587;

    pass="generated windows app code" Best regards, Minxin Yu

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.