Thursday, June 11, 2009

Custom WF Activity to send mobile text message in CRM 4.0

Creating a custom workflow activity for CRM is pretty straight forward. The approach we took was to register with an Email-to-SMS service provider to send out bulk SMS messages from CRM. The service provider provided us with an email account allowing the SMTP server to relay our messages through their server from the given account.

Following this approach all you need to do is simply send an email using the standard .NET libraries (System.Net.Mail) with a specific string format with a combination of the recipient's mobile number and service provider account (this will most defenitly be specific to your service provider). Sharepoint

Below I will give you some sample C# code to achieve this:




[CrmWorkflowActivity("Send SMS", "My Custom WF Activities")]
public partial class SendSMS: SequenceActivity
{

//DEV NOTE: Ensure that the name of the property (public string mobileNumber{}) match that of the name passed to DependencyProperty.Register
public static DependencyProperty mobileNumberProperty = DependencyProperty.Register("mobileNumber", typeof(string), typeof(SendSMS));

public static DependencyProperty messageProperty = DependencyProperty.Register("message", typeof(string), typeof(SendSMS));

[CrmInput("mobilenumber")]
[CrmDefault("")]
public string mobileNumber
{
get { return (string)base.GetValue(mobileNumberProperty); }
set { base.SetValue(mobileNumberProperty, value); }
}

[CrmInput("message")]
[CrmDefault("Type your sms message here")]
public string message
{
get { return (string)base.GetValue(messageProperty); }
set { base.SetValue(messageProperty, value); }
}

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext wfContext = contextService.Context;

ICrmService crmService = wfContext.CreateCrmService(true);

SendNewSMS(mobileNumber, message);

return ActivityExecutionStatus.Closed;
}





private void SendNewSMS(string mobileNr, string message)
{
string host = "localhost";

SmtpClient client = new SmtpClient(host);

MailAddress toAddress = new MailAddress(string.Format("{0}@myprovider.com", mobileNr));
MailAddress fromAddress = new MailAddress("account@myorg.com");

MailMessage msg = new MailMessage(fromAddress, toAddress);

msg.Subject = message;
msg.Body = string.Empty;

client.Send(msg);
}

No comments:

Post a Comment