windows workflow foundation初體驗__客製化活動

windows workflow foundation初體驗__客製化活動

看了書之後

Windows Workflow Foundation 新一代工作流程開發實務

來實作一下其中有關客製化活動部份

將之前寫的東西一起用

利用本機SMTP將信件寄出

 

以下是客製化活動的實現代碼

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Text;

namespace WorkflowModule
{
    public partial class SendMessage : System.Workflow.ComponentModel.Activity
	{
        [Flags]
        public enum MessageType
        {
           退回 = -1,
           待簽核 = 0,
           放行 = 1,           
           否決 = 2,
           代決 = 3
        };
		public SendMessage()
		{
			InitializeComponent();
		}
        public static DependencyProperty MailReceiverProperty = System.Workflow.ComponentModel.DependencyProperty.Register("MailReceiver", typeof(string), typeof(SendMessage));

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string MailReceiver
        {
            get
            {
                return ((string)(base.GetValue(SendMessage.MailReceiverProperty)));
            }
            set
            {
                base.SetValue(SendMessage.MailReceiverProperty, value);
            }
        }
        public static DependencyProperty MailSenderProperty = System.Workflow.ComponentModel.DependencyProperty.Register("MailSender", typeof(string), typeof(SendMessage));

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string MailSender
        {
            get
            {
                return ((string)(base.GetValue(SendMessage.MailSenderProperty)));
            }
            set
            {
                base.SetValue(SendMessage.MailSenderProperty, value);
            }
        }
        public static DependencyProperty DocNoProperty = System.Workflow.ComponentModel.DependencyProperty.Register("DocNo", typeof(string), typeof(SendMessage));

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string DocNo
        {
            get
            {
                return ((string)(base.GetValue(SendMessage.DocNoProperty)));
            }
            set
            {
                base.SetValue(SendMessage.DocNoProperty, value);
            }
        }
        public static DependencyProperty DocFlowStatusProperty = System.Workflow.ComponentModel.DependencyProperty.Register("DocFlowStatus", typeof(string), typeof(SendMessage));

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string DocFlowStatus
        {
            get
            {
                return ((string)(base.GetValue(SendMessage.DocFlowStatusProperty)));
            }
            set
            {
                base.SetValue(SendMessage.DocFlowStatusProperty, value);
            }
        }
        public static DependencyProperty DocMessageProperty = System.Workflow.ComponentModel.DependencyProperty.Register("DocMessage", typeof(MessageType), typeof(SendMessage));

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public MessageType DocMessage
        {
            get
            {
                return ((MessageType)(base.GetValue(SendMessage.DocMessageProperty)));
            }
            set
            {
                base.SetValue(SendMessage.DocMessageProperty, value);
            }
        }
        public static DependencyProperty MessageProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Message", typeof(string), typeof(SendMessage));

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Message
        {
            get
            {
                return ((string)(base.GetValue(SendMessage.MessageProperty)));
            }
            set
            {
                base.SetValue(SendMessage.MessageProperty, value);
            }
        }
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("LocalHost");
            //指定 Smtp 伺服器
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            //指定外送電子郵件的處理方式
            smtp.Credentials = new System.Net.NetworkCredential("", "");
            //利用帳號/密碼取得 Smtp 伺服器的憑證
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            //要發送的訊息
            msg.SubjectEncoding = System.Text.Encoding.Default;
            //主旨的編碼方式
            msg.BodyEncoding = System.Text.Encoding.Default;
            //訊息主體(郵件內容)的編碼方式
            msg.IsBodyHtml = false;
            //訊息主體(郵件內容)不要以 HTML 的方式呈現
            msg.From = new System.Net.Mail.MailAddress(MailSender);
            //寄件者
            msg.To.Add(new System.Net.Mail.MailAddress(MailReceiver));
            //收件者
            msg.Subject = DocNo;
            //主旨                        
            msg.Body = Message;
            //訊息主體(郵件內容)
            smtp.Send(msg);
            //發送 e-mail
            msg.Dispose();
            //釋放訊息所佔用的記憶體
            return base.Execute(executionContext);
        }

	}
}

測試結果

流程圖如下

pic3

將參數輸入

pic4

寄出郵件內容

pic5

注意事項

有關Property的代碼部分可以用插入程式碼片段來實現

pic2

pic2-2

pic2-3