Esempio n. 1
0
        // If testMailDropPath attribute is set, the email is written in files
        // in that path:
        //    xxxx.body.html with body
        //    xxxx.data.txt with message data (from, to, cc, bcc, and subject)
        private void WriteMailInTestDropPath(CodeActivityContext context)
        {
            // create file with Html of the body
            string testDropBodyFileName = string.Format("{0}\\{1}.body.htm", this.TestDropPath, DateTime.Now.ToString("yyyyMMddhhmmssff"));

            using (TextWriter writer = new StreamWriter(testDropBodyFileName))
            {
                writer.Write(this.Body);
            }

            // create file with from, to, cc, bcc, subject
            string testDropDataFileName   = string.Format("{0}\\{1}.data.txt", this.TestDropPath, DateTime.Now.ToString("yyyyMMddhhmmssff"));
            MailAddressCollection toList  = this.To.Get(context);
            MailAddressCollection bccList = this.Bcc.Get(context);
            MailAddressCollection ccList  = this.CC.Get(context);

            using (TextWriter writer = new StreamWriter(testDropDataFileName))
            {
                writer.Write("From: {0}", this.From.Get(context).Address);

                writer.Write("\r\nTo: ");
                foreach (MailAddress address in toList)
                {
                    writer.Write(string.Format("{0} ", address.Address));
                }

                if (TestMailTo.Expression != null)
                {
                    writer.WriteLine("\r\nTest MailTo Mode Enable...Address: {0}", TestMailTo.Get(context).Address);
                }

                if (ccList != null)
                {
                    writer.Write("\r\nCc: ");
                    foreach (MailAddress address in ccList)
                    {
                        writer.Write("{0} ", address.Address);
                    }
                }

                if (bccList != null)
                {
                    writer.Write("\r\nBcc: ");
                    foreach (MailAddress address in bccList)
                    {
                        writer.Write("{0} ", address.Address);
                    }
                }

                writer.Write("\r\nSubject: {0}", this.Subject.Get(context));
            }
        }
Esempio n. 2
0
        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            MailMessage message = new MailMessage();

            message.From = this.From.Get(context);

            if (TestMailTo.Expression == null)
            {
                foreach (MailAddress address in this.To.Get(context))
                {
                    message.To.Add(address);
                }

                MailAddressCollection ccList = this.CC.Get(context);
                if (ccList != null)
                {
                    foreach (MailAddress address in ccList)
                    {
                        message.CC.Add(address);
                    }
                }

                MailAddressCollection bccList = this.Bcc.Get(context);
                if (bccList != null)
                {
                    foreach (MailAddress address in bccList)
                    {
                        message.Bcc.Add(address);
                    }
                }
            }
            else
            {
                message.To.Add(TestMailTo.Get(context));
            }

            Collection <Attachment> attachments = this.Attachments.Get(context);

            if (attachments != null)
            {
                foreach (Attachment attachment in attachments)
                {
                    message.Attachments.Add(attachment);
                }
            }

            if (!string.IsNullOrEmpty(this.BodyTemplateFilePath))
            {
                LoadBodyTemplate(context);
            }

            if ((this.Tokens.Get(context) != null) && (this.Tokens.Get(context).Count > 0))
            {
                ReplaceTokensInBody(context);
            }

            if (this.TestMailTo.Expression != null)
            {
                AddTestInformationToBody(context);
            }

            message.Subject = this.Subject.Get(context);
            message.Body    = this.Body;

            SmtpClient client = new SmtpClient();

            client.Host      = this.Host;
            client.Port      = this.Port;
            client.EnableSsl = this.EnableSsl;

            if (string.IsNullOrEmpty(this.UserName))
            {
                client.UseDefaultCredentials = true;
            }
            else
            {
                client.UseDefaultCredentials = false;
                client.Credentials           = new NetworkCredential(this.UserName, this.Password);
            }

            if (!string.IsNullOrEmpty(this.TestDropPath))
            {
                WriteMailInTestDropPath(context);
            }

            var sendMailAsyncResult = new SendMailAsyncResult(client, message, callback, state);

            context.UserState = sendMailAsyncResult;
            return(sendMailAsyncResult);
        }