Esempio n. 1
0
        private void DoSomeWork(object status)
        {
            errorList = new StringBuilder();
            errorMail = false;
            string sSource = "MultiMail";
            string sLog    = "Application";

            errorList.AppendLine("Email not sent to:");
            IProgressCallback callback = status as IProgressCallback;

            try
            {
                callback.Begin(0, clbLeden.CheckedItems.Count + clbExtraEmail.CheckedItems.Count);

                SmtpClientExt client = new SmtpClientExt(param.STMPserver, param.STMPport, param.EmailUserId, param.EmailPassword,
                                                         EmailLogFile, chkLogEmail.Checked, ckbDoNotSendEmail.Checked, callback.SetText);

                MailMessage message = new MailMessage();
                message.Subject = txtSubject.Text;
                message.From    = new MailAddress(param.EmailReturnAdress);
                message.ReplyToList.Add(param.EmailReturnAdress);
                message.IsBodyHtml = ckbHtml.Checked;
                string messid = string.Format("<{0}@{1}>", Guid.NewGuid().ToString(), "wwww.ttvn.nl");
                message.Headers.Add("Message-Id", messid);

                string strBody = string.Empty;
                if (ckbHtml.Checked)
                {
                    strBody = string.Format(@"<html><head><meta http-equiv=Content-Type content=""text/html; charset=us-ascii""></head>{0}</html>", htmlTextbox1.Text);
                }
                else
                {
                    strBody = htmlTextbox1.PlainText;
                }

                if (txtBijlage1.Text != string.Empty)
                {
                    //emailer.Attachments.Add(new SmtpAttachment(txtBijlage1.Text));
                    message.Attachments.Add(new Attachment(txtBijlage1.Text));
                }
                if (txtBijlage2.Text != string.Empty)
                {
                    //emailer.Attachments.Add(new SmtpAttachment(txtBijlage2.Text));
                    message.Attachments.Add(new Attachment(txtBijlage2.Text));
                }
                if (txtBijlage3.Text != string.Empty)
                {
                    //emailer.Attachments.Add(new SmtpAttachment(txtBijlage3.Text));
                    message.Attachments.Add(new Attachment(txtBijlage3.Text));
                }

                for (int i = 0; i < clbLeden.Items.Count; i++)
                {
                    if (!clbLeden.GetItemChecked(i))
                    {
                        continue;
                    }

                    message.To.Clear();
                    message.To.Add(clbLeden.Items[i].ToString());
                    message.Body = MailRoutines.ReplaceKeyWords(strBody, ledenSchaduwlijst[i], param);



                    //emailer.To.Clear();
                    //emailer.To.Add(clbLeden.Items[i].ToString());
                    //emailer.Body = MailRoutines.ReplaceKeyWords(strBody, ledenSchaduwlijst[i], param);
                    try
                    {
                        //emailer.SendMessage();
                        client.Send(message);
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            if (!EventLog.SourceExists("MultiMail"))
                            {
                                EventLog.CreateEventSource(sSource, sLog);
                            }

                            EventLog.WriteEntry(sSource, ex.Message + @"/n" + message.Body,
                                                EventLogEntryType.Warning, 001);
                        }
                        catch (Exception ex2)
                        {
                            GuiRoutines.ExceptionMessageBox(this, ex);
                            Console.WriteLine(ex2.ToString());
                        }
                        errorMail = true;
                        errorList.AppendLine(clbLeden.Items[i].ToString());
                    }
                    callback.Increment(1);
                    callback.SetText(clbLeden.Items[i].ToString());
                    System.Threading.Thread.Sleep(50);
                    if (callback.IsAborting)
                    {
                        return;
                    }
                }


                for (int i = 0; i < clbExtraEmail.Items.Count; i++)
                {
                    if (!clbExtraEmail.GetItemChecked(i))
                    {
                        continue;
                    }

                    message.To.Clear();
                    message.To.Add(clbExtraEmail.Items[i].ToString());
                    message.Body = MailRoutines.ReplaceKeyWords(strBody, new tblLid(), param);

                    try
                    {
                        client.Send(message);
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            if (!EventLog.SourceExists("MultiMail"))
                            {
                                EventLog.CreateEventSource(sSource, sLog);
                            }
                            EventLog.WriteEntry(sSource, ex.Message,
                                                EventLogEntryType.Warning, 001);
                        }
                        catch { }

                        errorMail = true;
                        errorList.AppendLine(clbExtraEmail.Items[i].ToString());
                    }
                    callback.Increment(1);
                    callback.SetText(clbExtraEmail.Items[i].ToString());
                    System.Threading.Thread.Sleep(50);
                    if (callback.IsAborting)
                    {
                        return;
                    }
                }

                callback.WaitOK();
            }
            catch (System.Threading.ThreadAbortException ex)
            {
                // We want to exit gracefully here (if we're lucky)
                try
                {
                    if (!EventLog.SourceExists("MultiMail"))
                    {
                        EventLog.CreateEventSource(sSource, sLog);
                    }
                    EventLog.WriteEntry(sSource, ex.Message,
                                        EventLogEntryType.Warning, 001);
                }
                catch { }
            }
            catch (System.Threading.ThreadInterruptedException ex)
            {
                // And here, if we can
                try
                {
                    if (!EventLog.SourceExists("MultiMail"))
                    {
                        EventLog.CreateEventSource(sSource, sLog);
                    }
                    EventLog.WriteEntry(sSource, ex.Message,
                                        EventLogEntryType.Warning, 001);
                }
                catch { }
            }
            catch (Exception ex)
            {
                try
                {
                    if (!EventLog.SourceExists("MultiMail"))
                    {
                        EventLog.CreateEventSource(sSource, sLog);
                    }
                    EventLog.WriteEntry(sSource, ex.Message,
                                        EventLogEntryType.Warning, 001);
                }
                catch { }

                GuiRoutines.ExceptionMessageBox(this, ex);
            }
            finally
            {
                if (callback != null)
                {
                    callback.End();
                }
            }
        }
Esempio n. 2
0
        public frmMultiMail(object lijst, tblParameters Param, object templateName, string subject, List <string> attachmentList, bool OnlyFinancialEmail)
        {
            InitializeComponent();

            cmdGetMail.Visible = cmdSaveMail.Visible = false;

            if (lijst == null)
            {
                return;
            }

            //param = new tblParameters();
            param = Param;
            // De schaduwlijst dient om bij ieder email adres in de checkbox een Lid object te hebben zodat we de keywords kunnen vervangen
            ledenSchaduwlijst = new LedenLijst();

            //mostRecentlyUsed = new MosaicMru(this, "Multimail");
            //mostRecentlyUsed.ItemsSaveLimit = 10;
            //mostRecentlyUsed.ItemsAreFiles = false;
            //mostRecentlyUsed.BuildMenu(clbExtraEmail);
            try
            {
                foreach (string s in param.mmtxtExtraEmail)
                {
                    clbExtraEmail.Items.Add(s);
                }
            }
            catch { }

            _windowState = new PersistWindowState(this, @"Leden\Mail");
            //txtExtraEmail.Text = PersistControlValue.ReadControlValue(txtExtraEmail);
            chkLogEmail.Checked       = param.LogEmail;
            ckbDoNotSendEmail.Checked = param.DoNotSendEmail;

            EmailLogFile = param.LocationLogFiles + @"\" + param.ClubNameShort + "_Mail_Logfile.txt";
            if (!File.Exists(EmailLogFile))
            {
                File.Create(EmailLogFile);
            }


            #region Bouw Ledenlijst op
            if (lijst.GetType() == typeof(LedenLijst))
            {
                leden = (LedenLijst)lijst;
            }

            if (lijst.GetType() == typeof(tblLid))
            {
                tblLid lid = (tblLid)lijst;
                lid.Gemerkt = true;
                leden       = new LedenLijst(lid);
            }

            if (lijst.GetType() == typeof(List <EmailAdresLid>))
            {
                leden = new LedenLijst();
                foreach (EmailAdresLid e in (List <EmailAdresLid>)lijst)
                {
                    if (string.IsNullOrEmpty(e.EmailAddress.Trim()))
                    {
                        continue;
                    }
                    tblLid lid = new tblLid();
                    lid.Gemerkt = true;
                    lid.Email1  = e.EmailAddress;
                    leden.Add(lid);
                }
            }
            if (lijst.GetType() == typeof(tblRekening))
            {
                tblRekening rekening = (tblRekening)lijst;
                tblLid      lid      = rekening.Lid;
                lid.Gemerkt = true;
                leden       = new LedenLijst(lid);
            }

            #endregion

            try
            {
                foreach (tblLid lid in leden)
                {
                    if (lid.Gemerkt)
                    {
                        AddEmailToComboAndShadow(lid, OnlyFinancialEmail);
                    }
                }

                #region templateName == null dus we gebruiken de laatste opgeslagen default
                string body = string.Empty;
                if (templateName == null)
                {
                    mailFileName = PersistControlValue.ReadLocalAppSetting(@"Leden.Net", "LastUsedEmail");

                    cmdGetMail.Visible = cmdSaveMail.Visible = true;

                    if (mailFileName != string.Empty || !File.Exists(mailFileName))
                    {
                        StreamReader sr  = File.OpenText(mailFileName);
                        StringReader str = new StringReader(sr.ReadToEnd());
                        body = htmlTextbox1.Text = str.ReadToEnd();
                        sr.Close();
                        sr.Dispose();
                    }
                    else
                    {
                        body = htmlTextbox1.Text = mailFileName + " not found";
                    }
                    return;
                }
                #endregion

                #region TemplateNaam is ingevuld dus gaan we die gebruiken. igv lege string dan default mailtje
                // Het type is een string dus gaan we uit van een templatenaam
                if (templateName.GetType() == typeof(string))
                {
                    if ((string)templateName == string.Empty)
                    {
                        htmlTextbox1.Text = "Beste " + param.ClubNameShort + "-er,<br><br>Met vriendelijke groet,<br>" + param.ClubNameShort;
                        return;
                    }

                    // we maken gebruik van een template voor de leden. bijv Upas mail

                    //Inlezen van de template
                    string fileNameTemplate = param.LocationTemplates + @"\Template_" + (string)templateName + ".htm";;

                    if (!File.Exists(fileNameTemplate))
                    {
                        MessageBox.Show("Template mail " + fileNameTemplate + " bestaat niet", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    StreamReader sr1  = File.OpenText(fileNameTemplate);
                    StringReader str1 = new StringReader(sr1.ReadToEnd());
                    htmlTextbox1.Text = str1.ReadToEnd();
                }
                #endregion

                if (templateName.GetType() == typeof(BodyString))
                {
                    htmlTextbox1.Text = (BodyString)templateName;
                }

                //Replace keywords
                if (lijst.GetType() == typeof(tblLid))
                {
                    tblLid lid = (tblLid)lijst;
                    htmlTextbox1.Text = MailRoutines.ReplaceKeyWords(htmlTextbox1.Text, lid, param);
                }
                if (lijst.GetType() == typeof(tblRekening))
                {
                    tblRekening rekening = (tblRekening)lijst;
                    htmlTextbox1.Text = MailRoutines.ReplaceKeyWords(htmlTextbox1.Text, rekening, param);
                }

                #region Subject en Attachments
                txtSubject.Text = (subject != null ? subject : string.Empty);

                if (attachmentList != null)
                {
                    for (int i = 0; i < attachmentList.Count && i < 3; i++)
                    {
                        if (i == 0 && attachmentList[i] != string.Empty)
                        {
                            txtBijlage1.Text = attachmentList[i];
                        }
                        if (i == 1 && attachmentList[i] != string.Empty)
                        {
                            txtBijlage2.Text = attachmentList[i];
                        }
                        if (i == 2 && attachmentList[i] != string.Empty)
                        {
                            txtBijlage3.Text = attachmentList[i];
                        }
                    }
                }
                #endregion
            }
            catch { }
        }
Esempio n. 3
0
        private void SendInschrijfIncassoAankondiging()
        {
            string fileName = param.LocationTemplates + @"\Template_InschrijfIncasso.htm";

            if (!File.Exists(fileName))
            {
                MessageBox.Show("Template voor inschrijfgeldincasso mail " + fileName + " bestaat niet", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            SmtpClientExt client = new SmtpClientExt(param.STMPserver, param.STMPport, param.EmailUserId, param.EmailPassword,
                                                     EmailLogFile, chkLogEmail.Checked, ckbDoNotSendEmail.Checked, callback.SetText);

            //SmtpEmailer emailer = new SmtpEmailer();
            //emailer.Host = param.STMPserver;
            //emailer.From = param.EmailReturnAdress;
            //emailer.AuthenticationMode = AuthenticationType.Base64;
            //emailer.User = param.EmailUserId;
            //emailer.Password = param.EmailPassword;
            //emailer.LogFile = EmailLogFile;
            //emailer.DoNotSendMail = ckbDoNotSendEmail.Checked;
            //emailer.LogMail = chkLogEmail.Checked;

            StreamReader sr       = File.OpenText(fileName);
            StringReader str      = new StringReader(sr.ReadToEnd());
            string       template = str.ReadToEnd();

            foreach (Leden.Net.tblRekening rekening in selectedInc)
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(param.EmailReturnAdress);

                //emailer.ClearMessage();
                if (rekening.TypeRekening == 0)
                {
                    continue;                              // Geen Contributie incasso's
                }
                string sbr = template;
                //vervangen
                sbr = MailRoutines.ReplaceKeyWords(sbr, rekening.Lid, param);
                sbr = ReplaceRekeningItems(sbr, rekening);

                message.Body = sbr;
                //emailer.Body = sbr;
                if (rekening.Lid.MainEmailAdress != string.Empty)
                {
                    message.Subject = "Aankondiging incasso TTVN";
                    //emailer.Subject = "Aankondiging incasso TTVN";
                    message.To.Add(rekening.Lid.MainEmailAdress);
                    //emailer.To.Add(rekening.Lid.MainEmailAdress);
                }
                else
                {
                    message.Subject = "Geen email adres bij Aankondiging  incasso TTVN";
                    //emailer.Subject = "Geen email adres bij Aankondiging  incasso TTVN";
                    message.To.Add(param.EmailReturnAdress);
                    //emailer.To.Add(param.EmailReturnAdress);
                }

                message.IsBodyHtml = true;
                //emailer.SendAsHtml = true;
                try
                {
                    client.Send(message);
                    //emailer.SendMessage();
                }
                catch
                {
                    errorMail = true;
                    errorList.AppendLine(rekening.Lid.MainEmailAdress);
                }
                callback.Increment(1);
                callback.SetText(rekening.Lid.MainEmailAdress);
                System.Threading.Thread.Sleep(50);
                if (callback.IsAborting)
                {
                    return;
                }
            }
        }