Beispiel #1
0
        public void GenEmails()
        {
            try
            {
                if (this.senders.Count <= 0)
                {
                    GenericLogger <Generator> .Error(LocalizibleStrings.SendersCount);

                    return;
                }

                if (this.recipients.Count <= 0)
                {
                    GenericLogger <Generator> .Error(LocalizibleStrings.RecipientsCount);

                    return;
                }

                ParallelQuery <GenResult> results = this.recipients.AsParallel().SelectMany(recipient =>
                {
                    List <GenResult> genMsgs =
                        new List <GenResult>(this.countPerRecipient);

                    int genCount = 0;

                    while (genCount < this.countPerRecipient)
                    {
                        foreach (StoredContact sender in this.senders)
                        {
                            foreach (Storage.Message template in this.templates)
                            {
                                GenResult newMsg =
                                    this.EmailMessageFromMessage(
                                        sender,
                                        recipient,
                                        template);

                                if (newMsg.msg == null)
                                {
                                    return(genMsgs);
                                }

                                genMsgs.Add(newMsg);
                                genCount++;

                                if (newMsg.reply != null)
                                {
                                    genCount++;
                                }

                                if (newMsg.response != null)
                                {
                                    genCount++;
                                }

                                GenericLogger <Generator> .Message(
                                    LocalizibleStrings.MsgCreatedOf,
                                    genCount, this.countPerRecipient);

                                this.callback.BeginInvoke(null, null);

                                if (genCount == this.countPerRecipient)
                                {
                                    return(genMsgs);
                                }
                            }

                            if (genCount == this.countPerRecipient)
                            {
                                return(genMsgs);
                            }
                        }
                    }

                    return(genMsgs);
                });

                GenericLogger <Generator> .Message(LocalizibleStrings.MsgSendingStarted);

                results.ForAll(res =>
                {
                    try
                    {
                        res.msg.Send();
                        if (res.response != null)
                        {
                            res.response.Send();
                        }
                        if (res.reply != null)
                        {
                            res.reply.Send();
                        }
                    }
                    catch (Exception exc)
                    {
                        GenericLogger <Generator> .Message("Sending error " + exc);
                    }
                });
                GenericLogger <Generator> .Message(LocalizibleStrings.MsgSendingFinished);
            }
            catch (Exception exc)
            {
                GenericLogger <Generator> .Error(
                    LocalizibleStrings.ErrorGenMailMsg, exc);
            }
        }
Beispiel #2
0
        private GenResult EmailMessageFromMessage(
            StoredContact sender,
            StoredContact recipient,
            Storage.Message msg)
        {
            GenResult res = new GenResult();

            try
            {
                EmailAddress em =
                    Contact.Bind(this.service, new ItemId(sender.UniqId))
                    .EmailAddresses[EmailAddressKey.EmailAddress1];

                EmailMessage newMsg = new EmailMessage(this.service)
                {
                    From    = em,
                    Sender  = em,
                    Body    = new MessageBody(msg.BodyHtml),
                    Subject = msg.Subject
                };

                newMsg.Save(WellKnownFolderName.Drafts);

                foreach (object obj in msg.Attachments)
                {
                    Storage.Attachment attach = obj as Storage.Attachment;
                    if (attach == null)
                    {
                        continue;
                    }
                    newMsg.Attachments.AddFileAttachment(attach.FileName, attach.Data);
                }

                this.FillAdresses(ref newMsg, recipient.Email);

                res.msg = newMsg;

                // делаем ли форвард
                if (RndTrueFalse())
                {
                    newMsg.Update(ConflictResolutionMode.AlwaysOverwrite);
                    ResponseMessage respMsg = newMsg.CreateForward();
                    respMsg.BodyPrefix = @"test body prefix for forward message";
                    this.FillAdressesForResponse(ref respMsg, this.RndRecipMail());
                    respMsg.Save(WellKnownFolderName.Drafts);
                    res.response = respMsg;
                }

                // делаем ли реплай
                if (RndTrueFalse())
                {
                    /*newMsg.ReplyTo.Add(_RndRecipMail());
                     * if (_RndTrueFalse())
                     * {
                     *  newMsg.ReplyTo.Add(_RndRecipMail());
                     * }
                     *
                     * if (_RndTrueFalse()) */
                    {
                        newMsg.Update(ConflictResolutionMode.AlwaysOverwrite);
                        ResponseMessage replMsg = newMsg.CreateReply(RndTrueFalse());
                        replMsg.BodyPrefix = @"test body prefix for reply message";
                        this.FillAdressesForResponse(ref replMsg, recipient.Email);
                        replMsg.Save(WellKnownFolderName.Drafts);
                        res.reply = replMsg;
                    }
                }
            }
            catch (Exception exc)
            {
                GenericLogger <Generator> .Error(
                    LocalizibleStrings.ErrorCreateFromTemplate + msg.FileName, exc);
            }
            return(res);
        }