Esempio n. 1
0
        public bool EmailSender(string From, string To, string Subject, string Body, bool IsBodyHTML, int RepeatCount, ref string ErrorMessage)
        {
            int          c        = RepeatCount + 1;
            SMTPProperty smtpprop = new SMTPProperty();

            try
            {
                if (RepeatCount <= 3)
                {
                    smtpprop = GetActiveSmtpClient();
                    MailMessage mail = new MailMessage();
                    mail.From = new MailAddress(From);
                    mail.To.Add(To);
                    mail.Subject    = Subject;
                    mail.Body       = Body;
                    mail.IsBodyHtml = IsBodyHTML;
                    smtpprop.ISbusy = true;
                    smtpprop.SMTP.Send(mail);
                    smtpprop.ISbusy = false;
                    ErrorMessage    = "";
                    return(true);

                    //SmtpClient smtp = new SmtpClient(SMTPserverAddress, Port);
                    //smtp.Timeout = 1000000;
                    //smtp.EnableSsl = false;
                    //smtp.UseDefaultCredentials = false;
                    //smtp.Credentials = new NetworkCredential(CridentionalUserName, Password);
                    //MailMessage mail = new MailMessage();
                    //mail.From = new MailAddress(From);
                    //mail.To.Add(To);
                    //mail.Subject = Subject;
                    //mail.Body = Body;
                    //mail.IsBodyHtml = IsBodyHTML;
                    //smtp.Send(mail);
                    //ErrorMessage = "";
                    //smtp.Dispose();
                    //return true;
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception Ex)
            {
                EmailBankCore.LogRegister("EmailSender To : " + To, Ex.Message);
                //smtpprop.ISbusy = false;
                ErrorMessage = Ex.Message;
                System.Threading.Thread.Sleep(1000);
                bool bl = EmailSender(From, To, Subject, Body, IsBodyHTML, c, ref ErrorMessage);
                return(bl);
            }
        }
Esempio n. 2
0
 public SMTPProperty GetActiveSmtpClient()
 {
     try
     {
         if (SMTPList.Count <= 0)
         {
             SmtpClient   smtp  = GetNewSMTP();
             SMTPProperty smtpp = new SMTPProperty {
                 SMTP = smtp, ISbusy = false, TTL = Environment.TickCount
             };
             SMTPList.Add(smtpp);
             return(SMTPList[SMTPList.Count - 1]);
         }
         else
         {
             int i = 0;
             while (SMTPList[i].ISbusy)
             {
                 i++;
                 if (i >= SMTPList.Count)
                 {
                     if (i <= 50)
                     {
                         SmtpClient   smtp  = GetNewSMTP();
                         SMTPProperty smtpp = new SMTPProperty {
                             SMTP = smtp, ISbusy = false, TTL = Environment.TickCount
                         };
                         SMTPList.Add(smtpp);
                         return(SMTPList[SMTPList.Count - 1]);
                     }
                     else
                     {
                         System.Threading.Thread.Sleep(1000);
                         return(GetActiveSmtpClient());
                     }
                 }
             }
             long Tick = Environment.TickCount;
             if ((-SMTPList[i].TTL) > 1000000)
             {
                 SmtpClient   smtp  = GetNewSMTP();
                 SMTPProperty smtpp = new SMTPProperty {
                     SMTP = smtp, ISbusy = false, TTL = Environment.TickCount
                 };
                 SMTPList[i] = smtpp;
             }
             return(SMTPList[i]);
         }
     }
     catch (Exception ex)
     {
         EmailBankCore.LogRegister("GetActiveSmtpClient", ex.Message);
         SmtpClient smtp = new SmtpClient(SMTPserverAddress, Port);
         smtp.UseDefaultCredentials = false;
         smtp.EnableSsl             = false;
         smtp.Credentials           = new NetworkCredential(CridentionalUserName, Password);
         SMTPProperty smtpp = new SMTPProperty {
             SMTP = smtp, ISbusy = false, TTL = Environment.TickCount
         };
         return(smtpp);
     }
 }