using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
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.Net.Mail;
using System.Configuration;
namespace SampleWorkflow_LM
{
/// <summary>
/// author:praveen alwar
/// </summary>
public sealed partial class Requestworkflow : SequentialWorkflowActivity
{
/// <summary>
/// hashtable to hold the email data
/// </summary>
private Hashtable _requestordata;
public Hashtable requestorleavedata
{
get { return _requestordata; }
set { _requestordata = value; }
}
//mailer class to be sent to the smtp server
private MailData _maildata = new MailData();
/// <summary>
/// this method will be invoked from the approver notification code activity
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NotifyApprover(object sender, EventArgs e)
{
try
{
////Send email to Approver
fillMailClass(0);
bool ismailsent = SendMail(_maildata);
}
catch (Exception ex)
{
//log the error
}
}
/// <summary>
/// this method will be invoked from the applier notification code activity
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NotifyApplier(object sender, EventArgs e)
{
try
{
////Send email to Applier
fillMailClass(1);
bool ismailsent = SendMail(_maildata);
}
catch (Exception ex)
{
//log the error
}
}
private void fillMailClass(int mailtype)
{
//if mailtype ->0 then approver notification, mail type->1 , then notify applier
switch (mailtype)
{
case 0:
#region build the email details class for approver
_maildata.MailTo = requestorleavedata["rmemail"].ToString() ;
_maildata.MailSubject = ConfigurationManager.AppSettings["NewLeaveRequest"].ToString();
_maildata.MailFrom = ConfigurationManager.AppSettings["mailfrom"].ToString();
_maildata.MailBody = requestorleavedata["emailbody"].ToString();
//_maildata.IsHtmlBody = true;
#endregion
break;
case 1:
#region build the email details class for resource
_maildata.MailTo = requestorleavedata["resourceemail"].ToString();
#endregion
break;
}
}
private static bool SendMail(MailData emailDetails)
{
//Instantiate the MailMessage Class from the framework.Mail message class represents the email that has to be sent
MailMessage message = new MailMessage();
//counter is used to check the number of MAILTO instances
try
{
//Read the message from settings
message.From = new MailAddress(emailDetails.MailFrom);
//Check if the mailto is not null
if (emailDetails.MailTo != null)
{
// Add all recipent of mail
message.To.Add(new MailAddress(emailDetails.MailTo));
}
message.Body = emailDetails.MailBody;
//check if the email has to be sent in HTML format
message.BodyEncoding = System.Text.Encoding.Unicode;
//assign the mail subject
message.Subject = emailDetails.MailSubject;
//double-check if mail to has been supplied. atleast one email address has to be provided.
if (emailDetails.MailTo != null)
{
//Instantiate the SMTP client to send the email
SmtpClient client = new SmtpClient();
client.Send(message);
}
else
{
//if the mail to is not specified, log error and return false
//log the error
return false;
}
}
catch (SmtpException ex)
{
//catch and log the error
ex.Data.Add("Error sending mail", ex.InnerException.Message);
//log the error
return false;
}
catch (Exception ex)
{
//catch and log the error
ex.Data.Add("Error sending mail", ex.InnerException.Message);
//log the error
return false;
}
finally
{
message.Dispose();
}
return true;
}
public Requestworkflow()
{
InitializeComponent();
}
}
}Leave management workflow using Windows Workflow Foundation -Part I . Please refer to the article/code description at this link: http://www.c-sharpcorner.com/UploadFile/praveenalwar/understanding-windows-workflow-foundation-leave-management/
