Proof-of-concept: forward any incoming email message to web app, as an HTTP POST
For the last couple of days I've been testing a number of online services. One that caught my attention was http://cloudmailin.com/:
...as soon as CloudMailin receives an email it will be sent to your website as an HTTP POST. It's exactly the same as a user filling in a form on your site."
This is such a great and "simple" idea. For online sites this service work like a charm. It’s easy to setup and use.
But what about in-house applications, like your company customer helpdesk application? So let’s build our sample to achieve this goal.
First we will start by creating a new console application in Visual Studio that will responsible for monitoring new incoming messages.
To establish the connection to the mail server I'll be using the trial version of Maill.dll. So after the project was created I'll install the mail.dll thru Nuget:
Next we create a class called MailMonitor and add the following code:
As you can see from the code above, on line 15 thru 46 we:
· Create our IMAP client and login to the server. In this sample I’m connecting to Google Mail. In a corporate environment we should could be connecting to a Microsoft Exchange Server for instance.
· We “navigate” to the Inbox folder and wait for new incoming messages.
· On new messages we start a async task that will post the email content to our web application.
On line 6 thru 13 we define our async action to be executed on each new incoming email.
On our program.cs we create a new instance of the MailMonitor class and call the handler method:
Now that we have our Monitor, let’s build a simple web application that will receive the POST. We start be creating a new ASP.NET MVC3 Web Application. Let’s spice the things a bit: Since we don’t have any database on this sample, we will use SignalR to interact with our UI. (For those that don’t know SignalR, it’s a “Async library for .NET to help build real-time, multi-user interactive web applications”. Visit this post from Scott Hanselman for a more detailed sample of SignalR)
So first of all we will use once again Nuget, now on our web app, to add the SignalR dependencies:
Next on the HomeController add the following actions:
We now need to create our SignalR Hub:
Finally we add a view for our controller action:
And there it is. To test it just run the app, send an email and watch it being delivered to the web app in real-time.
Sample code now (finally) online!
Finally got some spare time to pickup some sample code I've worked in the past and placed it here. I'll keep updating this new area, so keep watching out.
How to import an Excel sheet into multiple SQL tables using SSIS
Today we will be looking on how to import an excel sheet into multiple tables. Let’s take as an example the following excel spreadsheet:
Looking at the excel above we can identify two problems:
1) We need to split a sheet into three tables
2) No identifier columns
For this task we will be using SQL Server Integration Services. So let’s run the SQL Server Business Intelligence Development Studio and create a new Integration Services Project:
Now let’s create our data flow:
First of all we need to establish a connection to our Excel file. On the bottom , right-click on the Connection Managers and create a New Connection:
Next we will choose the Excel connection and point to our source file:
Now that we have our connection we need to add a Excel Source to our Data Fow:
Next double-click on the Excel source to change its settings like so:
If all is well we should be able to preview the data:
Now, one of the problems we identified earlier was the lack of a identifier column for both stores and products. So first we need to solve this with the help of the Derived Column Data Flow Transformation:
First we connect the Excel Source with the Derived Column. Then we double-click the Derived Column and configure it like so:
Ok, we now have the identifier columns but still the is no value for them. Let’s fill that gap. We need to ensure that every time the Excel is imported the same key is generated. We can do that with the help of MD5. So let’s add a Script Component to our Data Flow, setup and add the c# code for this task:
1) First we need to connect the Derived Column to our Script Component;
2) Secondly when we double-click the Script Component we must choose the Transformation Mode;
3) Next we go to the Input Columns and choose the new Derived Columns and also the STORE and COD Columns;
4) Finally we add our code to the component:
And the code:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
Row.ProductId = Encode(Row.COD.ToLower());
Row.StoreId = Encode(Row.STORE.ToLower());
}
private Guid Encode(string text)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(text);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
text = s.ToString();
return new Guid(text);
}
Like I said before, we will be using MD5 crypto to generate our unique columns. Sweet. One problem solved.
Now that we got our final source table we need to execute multiple operations on it. For that we will be using the MultiCast Transformation:
So, first lets populate the Products Table. We need a way to get the list of distinct products. For that we will use the Sort Transformation:
We first select the columns that we want to use and most importantly we must select “Remove rows with duplicate sort values” to have our distinct filter.
Let’s try it to see if it works. For that we will be adding a Grid Data Viewer to the connection between the Script Component and the Multicast. We, for test purpose only, will also add a Data Reader Destination and connect the Sort Transformation to the Data Reader Destination. Then will add a new Data Viewer to the connection.
So to add a Data Viewer simple right-click on the connection between components, click Add, select Grid type and click OK twice:
Notice now that we have a little goggles between our components:
So let’s run it:
When running we will be presented with a grid for the first Data viewer. Notice that our derived columns are now filled with unique values:
Cool.
The next Data Viewer is for the distinct products:
Nice.
Also notice that the data flow show the number of rows returned in each step:
Ok. The hard part is done. We now pretty much have to do a copy paste to fill the Store table:
We now have the distinct products and stores. Finally we will add the ADO NET Destination for both:
In the end:
Hope it helps J
ASP.net Webforms and the Passive View Design Pattern
This is more a personal reminder than an article.
From the view we can get the typed user name and set a message.
Now, let's update our default.aspx to become our view. First we need to add a textbox, named txtUserName, a button, named btnGreetUser and a literal that will hold our greeting message (named litMessage).
Next, in the code-behind file of our default.aspx page, we add the IHomeView and implement its properties:
Notice that:
- The page_load event of the page initializes a Presenter passing the instance of the view;
- Our View properties map to both the textbox, for getting the name, and to the literal, for setting the message;
- The button click handler calls a method of the Presenter.
Now that we've build the View we can't yet run the code: The Presenter is missing.
As I stated before, the Presenter will handle all te responses to user events, in this case, the button click event. So based on the needs of the view, our presenter will look like so:
Taking a closer look at the OnGreetUser() we can see that we take the user name from the view, build a simple greeting message and set back the message to the view thru the Message property of the view.
"That's all folks..."
We're done. If we run our application we can see how everything is hook up and a nice message is shown to the user when we types his name and presses the button.
Since there's a separation of the UI logic from the view implementation we can easly test our code:
Goo.gl Url Shortener API Wrapper
Just for the fun, there's a Goo.gl Url Shortener API wrapper on my codeplex corner.
GooglErrorResponse error = null;
var url = service.GetShortUrl("www.google.com", out error);
Console.WriteLine(url.Id);
var details = service.GetShortUrlDetails(url.Id, out error);
Console.WriteLine(details.Created);
Book review: A Guide to Claims-Based Identity and Access Control
It's been a while since my last book review. Today's book is A Guide to Claims-Based Identity and Access Control
First I would like to say thanks to Eugenio Pace (@eugenio_pace) for sending me the printed version, and to O'Reilly Media (@oreillymedia) for the digital version.using jQueryUI with Script# and the Script# Contrib project roadmap
Since my last post, I've started a new codeplex project to allow the usage of the jQueryUI in Script#.Getting started with Script# (ScriptSharp)
If you, like me, develop web applications, you got your hands all dirty with JavaScript.Script# brings productivity to Ajax and JavaScript development. Script# is a free tool that enables developers to author C# source code and subsequently compile it into regular script that works across all modern browsers, and in doing so, leverage the productivity and power of existing .NET tools as well as the Visual Studio IDE. Script# empowers you with a development methodology and approach that brings software engineering, long term maintainability and scalable development approaches for your Ajax applications, components and frameworks.
Script# is used extensively by developers within Microsoft building Ajax experiences in Windows Live, Office to name just a couple, as well as by a external developers and companies including Facebook. If you’re building Ajax-based RIA applications, you owe it to yourself to try Script# today and see if it can help improve your own Ajax development!
- The code we made in C# is almost identical to the code generated;
- All the client-side objects, like Window and Document, are available to C# code, so there is no new learning involved and with the benefit of compile time checks;
- All the documentation added to the C# code is also added to the JavaScript (on the debug file);
- We are working on a OO code on the client-side.
jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.
Book of the month: Programming Microsoft(R) ASP.NET MVC by Dino Esposito





















