Failure sending mail.

Apex6554 60 Reputation points
2023-04-02T20:46:28.94+00:00
         string from, pass, messageBody;
            Random rand = new Random();
            randomCode = (rand.Next(999999)).ToString();
            MailMessage message = new MailMessage();
            to = (txtEmail.Text).ToString();
            from = "*********@gmail.com";
            pass = "***********";
            messageBody = "your reset code is" + randomCode;
            message.To.Add(to);
            message.From = new MailAddress(from);
            message.Body = messageBody;
            message.Subject = "Password resetting code";
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            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
C#
C#
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
    2023-04-02T22:40:19.7333333+00:00

    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()
    {
       try
       {
          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(body);
          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.To.Add(emailTo);
             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:\SomeZip.zip"));
    
             using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
             {
                if (UseDefaultCredentials)
                {
                   smtp.UseDefaultCredentials = true;
                   //smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
                }
                else
                {
                   smtp.Credentials = new NetworkCredential(emailFrom, password);
                }
    
                smtp.EnableSsl = enableSSL;
                smtp.Send(mail);
             }
          }
    
          Console.WriteLine("An e-mail has been successfully sent to: {0}.", emailTo);
       }
       catch (Exception ex)
       {
          Console.WriteLine(ex.ToString());
       }
    }
    
    // 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));
       }
    }
    
    
    <configuration>
       <appSettings>
          <add key="smtpAddress" value="smtp.mail.yahoo.com"/>
          <add key="portNumber" value="587"/>
          <add key="enableSSL" value="true"/>
          <add key="emailFrom" value="ykhabins@yahoo.com"/>
          <add key="password" value="Password"/>
          <add key="UseDefaultCredentials" value="false"/>
    
          <add key="emailTo" value="ykhabins@gmail.com"/>
          <add key="subject" value="Hello"/>
          <add key="body" value="This is e-mail testing via SMTP protocol."/>
       </appSettings>
    </configuration>
    
    0 comments No comments

  2. Minxin Yu 10,441 Reputation points Microsoft Vendor
    2023-04-04T07:10:59.17+00:00

    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.