예제 #1
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 SendEmailAsyncResult = new SendEmailAsyncResult(client, message, callback, state);

            context.UserState = SendEmailAsyncResult;
            return(SendEmailAsyncResult);
        }
예제 #2
0
        protected override void Cancel(AsyncCodeActivityContext context)
        {
            SendEmailAsyncResult SendEmailAsyncResult = (SendEmailAsyncResult)context.UserState;

            SendEmailAsyncResult.Client.SendAsyncCancel();
        }