C Simple Mass Mailing Generic Class

  • 2021-09-12 01:52:26
  • OfStack

This article examples for everyone to introduce C # Mail Group Sending General Class, for your reference, the specific content is as follows


public static class Email
  {
    /// <summary>
    ///  Sender 
    /// </summary>
    public static string mailFrom { get; set; }

    /// <summary>
    ///  Recipient 
    /// </summary>
    public static string[] mailToArray { get; set; }

    /// <summary>
    ///  CC 
    /// </summary>
    public static string[] mailCcArray { get; set; }

    /// <summary>
    ///  Title 
    /// </summary>
    public static string mailSubject { get; set; }

    /// <summary>
    ///  Text 
    /// </summary>
    public static string mailBody { get; set; }

    /// <summary>
    ///  Sender password 
    /// </summary>
    public static string mailPwd { get; set; }

    /// <summary>
    /// SMTP Mail server 
    /// </summary>
    public static string host { get; set; }  

    /// <summary>
    ///  Mail server port 
    /// </summary>
    public static int port { get; set; }

    /// <summary>
    ///  Whether the body is html Format 
    /// </summary>
    public static bool isbodyHtml { get; set; }

    /// <summary>
    ///  Annex 
    /// </summary>
    public static string[] attachmentsPath { get; set; }

    public static bool Send()
    {
      // Class using the specified mail address MailAddress Instances 
      MailAddress maddr = new MailAddress(mailFrom);

      // Initialization MailMessage Instances 
      MailMessage myMail = new MailMessage();

      // Add a mail address to the recipient address collection 
      if (mailToArray != null)
      {
        for (int i = 0; i < mailToArray.Length; i++)
        {
          myMail.To.Add(mailToArray[i].ToString());
        }
      }

      // Add a mail address to the Cc Recipient Address Collection 
      if (mailCcArray != null)
      {
        for (int i = 0; i < mailCcArray.Length; i++)
        {
          myMail.CC.Add(mailCcArray[i].ToString());
        }
      }
      // Sender's address 
      myMail.From = maddr;

      // The title of the e-mail message 
      myMail.Subject = mailSubject;

      // The encoding used for the subject content of the e-mail message 
      myMail.SubjectEncoding = Encoding.UTF8;

      // Email body 
      myMail.Body = mailBody;

      // Encoding of e-mail body 
      myMail.BodyEncoding = Encoding.Default;

      // Email priority 
      myMail.Priority = MailPriority.High;

      // E-mail is not html Format 
      myMail.IsBodyHtml = isbodyHtml;

      // Add annexes if they exist 
      try
      {
        if (attachmentsPath != null && attachmentsPath.Length > 0)
        {
          Attachment attachFile = null;
          foreach (string path in attachmentsPath)
          {
            attachFile = new Attachment(path);
            myMail.Attachments.Add(attachFile);
          }
        }
      }
      catch (Exception err)
      {
        throw new Exception(" Error adding attachment :" + err.Message);
      }

      SmtpClient client = new SmtpClient();

      // Specify the sender's mail address and password to verify the sender's identity 
      client.Credentials = new NetworkCredential(mailFrom, mailPwd);

      // Settings SMTP Mail server 
      //client.Host = "smtp." + myMail.From.Host;
      client.Host = host;

      //SMTP Mail server port 
      client.Port = port;

      // Whether to use a secure connection 
      //client.EnableSsl = true;

      try
      {
        // Send a message to SMTP Mail server 
        client.Send(myMail);
        return true;
      }
      catch (SmtpException ex)
      {
        string msg = ex.Message;
        return false;
      }

    }

I hope this article is helpful for everyone to learn C # programming.


Related articles: