Example #1
0
        public static string getRandomBody(Random r, BodyCompositionRules bcr)
        {
            StringBuilder sb                   = new StringBuilder();
            int           numwords             = r.Next(10, maxWordsInBody + 1);
            int           wordsInSentence      = 1;
            int           sentencesInParagraph = 1;

            bcr.currentSentenceLength  = r.Next(3, 18);
            bcr.currentParagraphLength = r.Next(2, 6);

            for (int i = 0; i < numwords; i++)
            {
                string word = words[r.Next(words.Count)];
                if (wordsInSentence == 1)
                {
                    string capitalFirstLetter = word.Substring(0, 1).ToUpper(System.Globalization.CultureInfo.CurrentCulture);

                    sb.Append(capitalFirstLetter);
                    sb.Append(word.Substring(1));
                }
                else
                {
                    sb.Append(word);
                }

                if (wordsInSentence % bcr.currentSentenceLength == 0)
                {
                    sb.Append(". ");
                    bcr.currentSentenceLength = r.Next(3, 18);
                    wordsInSentence           = 1;
                    sentencesInParagraph++;
                }
                else
                {
                    if (i == numwords - 1) // last word we are going to add put a period.
                    {
                        sb.Append(".");
                    }
                    else
                    {
                        sb.Append(" ");
                    }

                    wordsInSentence++;
                }
                if (sentencesInParagraph % bcr.currentParagraphLength == 0)
                {
                    sb.Append("\r\n\r\n");
                    bcr.currentParagraphLength = r.Next(2, 6);
                    sentencesInParagraph       = 1;
                }
            }

            return(sb.ToString());
        }
Example #2
0
        public static Attachment getRandomAttachment(Random r, BodyCompositionRules bcr)
        {
            string     attachmentName = getRandomSubject(r).Replace("'", "").TrimEnd();
            Attachment attach;

            if (r.Next(1, 101) > 30)
            {
                string attachmentText = getRandomBody(r, bcr);
                var    stream         = new MemoryStream(Encoding.UTF8.GetBytes(attachmentText));
                attach = new Attachment(stream, attachmentName + ".txt");
            }
            else
            {
                // 30 percent chance we want binary attach
                // make attach 900 bytes - ~1.5 MB
                byte[] bytes = new byte[r.Next(900, 1500000)];
                r.NextBytes(bytes);
                var stream = new MemoryStream(bytes);

                attach = new Attachment(stream, attachmentName + ".binary");
            }
            return(attach);
        }
Example #3
0
        // this method is called from Parallel Invoke, so multiple threads will be executing this concurrently
        private static void SendMailFromUser(SendMailFromUserArgs args)
        {
            var fromMu = args.User;

            // make sure creds are set and setup SMTP client instance for sender to use this session..

            if (!(fromMu.networkCred is NetworkCredential))
            {
                fromMu.networkCred = new NetworkCredential(fromMu.sAMAccountName, args.defaultPassword, DirectoryEntryContext.DefaultDomain);
            }

            using (var curSmtpClient = new SmtpClient(MailUsers.dblist[fromMu.exchangeDBIndex].CurrentServer, 587))
            {
                curSmtpClient.Credentials = fromMu.networkCred;

                var from     = new MailAddress(fromMu.smtpAddress, fromMu.displayName);
                var progress = args.progress;

                var bw            = args.backgroundWorker;
                var dwea          = args.doWorkEventArgs;
                var r             = args.randomInstance;
                var mailBodyStyle = new BodyCompositionRules();

                // only trace here if worker is not being cancelled.  Otherwise if verbose tracing is enabled it can hang UI.
                if (!dwea.Cancel)
                {
                    ts.TraceEvent(TraceEventType.Verbose, 0, "Starting sending mail from {0}", fromMu.displayName);
                }

                foreach (var mal in senderAssignments[fromMu])
                {
                    // check for user cancellation

                    if (bw.CancellationPending)
                    {
                        if (!dwea.Cancel)
                        {
                            dwea.Cancel = true;
                        }

                        Interlocked.CompareExchange(ref progress.cancelled, 1, 0);

                        bw.ReportProgress(0, progress);
                        return;
                    }

                    // check for too many errors

                    if (progress.consecutiveErrorsSending >= maxConsecutiveFailuresSending)
                    {
                        if (!dwea.Cancel)
                        {
                            dwea.Cancel = true;
                        }

                        if (Interlocked.Exchange(ref progress.cancelled, 2) == 0)
                        {
                            //only trace this once, if not already pending cancel
                            ts.TraceEvent(TraceEventType.Critical, 0, "Stopping after {0} consecutive errors", maxConsecutiveFailuresSending);
                        }

                        bw.ReportProgress(0, progress);
                        return;
                    }

                    // compose message

                    var message = new MailMessage(from, mal[0]);
                    if (mal.Count > 1)
                    {
                        for (int i = 1; i < mal.Count; i++)
                        {
                            message.To.Add(mal[i]);
                        }
                    }
                    message.Body    = getRandomBody(r, mailBodyStyle);
                    message.Subject = getRandomSubject(r);

                    // attachments

                    if (r.Next(1, 101) > (100 - args.percentChanceOfAttachments))
                    {
                        message.Attachments.Add(getRandomAttachment(r, mailBodyStyle));
                        if (r.Next(1, 101) > 50) // 50 percent chance of more than one attachment.
                        {
                            var attachments = r.Next(1, args.maxAttachments + 1);

                            for (int i = 1; i < attachments; i++) // start i=1 because we already have 1 attachment
                            {
                                message.Attachments.Add(getRandomAttachment(r, mailBodyStyle));
                            }
                        }
                    }

                    // send

                    try
                    {
                        curSmtpClient.Send(message);
                        Interlocked.Increment(ref progress.messagesSent);
                        Interlocked.Exchange(ref progress.consecutiveErrorsSending, 0);
                    }
                    catch (Exception e)
                    {
                        Interlocked.Increment(ref progress.errorsSending);
                        Interlocked.Increment(ref progress.consecutiveErrorsSending);
                        ts.TraceEvent(TraceEventType.Error, 0, "sending message from user {0}.  {1}", fromMu.displayName, e.ToString());
                    }
                    finally
                    {
                        if (message != null)
                        {
                            message.Dispose();
                        }
                    }


                    bw.ReportProgress(0, progress);
                }

                Interlocked.Increment(ref progress.sendersDone);
                ts.TraceEvent(TraceEventType.Verbose, 0, "Finished sending from {0}", fromMu.displayName);
                bw.ReportProgress(0, progress);
            }
        }