SoFunction
Updated on 2025-03-07

Example of C# SMTP sending mail

In program development, there is usually a need to push messages, usually SMS services, emails, and phone reminders.

SMS and phone reminders usually require purchasing services from the operator to call the interface, which is more troublesome. Email information push is also a good choice. Use C# to implement SMTP to send emails

Copy the code/// <summary>
    /// Send email    /// </summary>
    /// <param name="M">Sentence content</param>    public static void LocalHostSend(SendMail M)
    {
      try
      {
        MailMessage myMail = new MailMessage();//Send email        
        foreach (string item in )//Add recipient        {
          (item);
        }
        foreach (string item in )//Add cc        {
          (item);
          
        }

         = ;//Email Subject         = ;//Email title code
         = new MailAddress(, , );//Send information

         = ;//Email content         = ;//Email content encoding         = ;//Is it HTML mail?         = ;//Email priority
        SmtpClient smtp = new SmtpClient();//SMTP protocol
         = ;//Security encryption is required to use QQ mailbox         = ;

         = ;//Host
         = new NetworkCredential(, );//Verify the sender information
        (myMail);//send
      }
      catch (Exception e)
      {
        ();
      }
      
    }
public class SendMail
  {
    public string From { get; set; }//Sender address    public string Password { get; set; }//password    public string [] Addressee { get; set; }//Recipient address    public string [] CC { get; set; }//Check    public string Theme { get; set; }//theme    public string DisplayName { get; set; }//Sender name    public Encoding SubjectEncoding { get; set; }//coding    public string Body { get; set; }//Email content    public Encoding BodyEncoding { get; set; }//Email content encoding    public bool IsBodyHtml { get; set; }//Is HTML mail    public MailPriority Priority { get; set; }//Email priority    public bool EnableSsl { get; set; }//Is it ssl    public bool UseDefaultCredentials { get; set; }
    public string Host { get; set; }

  }

The above is the detailed content of the example of sending emails by C# SMTP. For more information about sending emails by C# SMTP, please follow my other related articles!