How to test email functionality using Smtp Client

Writing code for sending mail is one of the common tasks that we need to work every now and again. Sometimes it is one of the requirements in our application and some time we develop to send alerts to all when application looses its control on production :).

.NET made it very simple to send the mail which normally looks a complicated task. We can easily write 4-5 lines of the code to send the mail. Lets just have a look on a sample code

// Creating MailMessage object with From and To address
MailMessage message = new MailMessage("brij.mishra@outlook.com", "abc@yahoo.com");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "Test mail using SmtpClient";
message.Body = "Details of test mail using SmtpClient";

// Add more details to your message object

// Creating SmtpClient object
SmtpClient client = new SmtpClient("Provide smtp server details");
client.UseDefaultCredentials = true;
try
{
    client.Send(message);
}
catch (Exception exception)
{
    // Log Exception
}

Above code snippet is just to send a simple mail (System.Net). But It also provides rich APIs which can be used to leverage the features like sending attachment, set body as html etc.

One of the key points in the code that it uses a smtp server to send the mail. That smtp server must be configured so that it can be used. Normally this kind of configuration we used to have on production or Pre-production environment. But what if we want to send from our local machine or integration server or test our functionality. It becomes very important because most of the time it has been found that look and feel of the body gets changed when it reaches in inbox or it looks way different when we open the email. So we need to see verify mail format then only we can assure about the functionality. Similarly there could be many other things to verify.

The Good news is that we have a very simple way which can be used to test the email functionality. We just need to add the following configuration on web.config.

  <system.net>
    <mailSettings>
      <!-- Setting delivery method as SpecifiedPickupDirectory-->
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <!-- Setting the folder path-->
        <specifiedPickupDirectory pickupDirectoryLocation="C:\Data\TestMails\"/>
      </smtp>
    </mailSettings>
  </system.net>

If we closely look the configuration, here we find that in smtp tag the deliveryMethod is set as  SpecifiedPickupDirectory then we added the specifiedPickupDirectory tag and provided the pickupDirectoryLocation  as the folder where we want to save the mails.Now the mails should be saved in provided folder. Lets run the code and see the folder

folderHere we can see that there is a file got saved in eml format. Lets open it

mailNow no matter whatever smtp details we provide in our code (see in the sample code, we just put the normal explanatory text), the mail would be saved in provided folder in configuration. SMTP Client checks the details from configuration if it is available then acts accordingly. if it is available save the file else try to contact the server given and if there is any issue throws an exception.

Hope this help you while testing.

Cheers
Brij

Dependency Injection Annotation Process in {{AngularJS}} – Part 18

Hello All,

In last two post, we discussed about DI, various types available in AngularJS and some insights around it. It is going to be another post on DI itself but here we’ll talk about annotations.I would recommend to go through last two posts before going further.

  1. Dependency Injection (DI) in {{AngularJS}} – Part 16
  2. Dependency Injection (DI) in {{AngularJS}} Contd.– Part 17

Note – This post is a part of series of posts on AngularJS and it is 18th one. Click here to view all the post. Apart from that you can also use the navigation links for next and previous post in each post at the bottom to check all.

In our previous posts, we have seen that services are passed as a parameter with same name  which was used defining like Circle and this is crucial because Angular internally locate the services using the name itself. So if the name does not match then dependencies won’t be resolved. As the name should be same so minifying the script would break the application which cannot be accepted. So let’s first see that the number of options provided by AngularJS.

AngularAnnSo as we have seen, we have three ways to inject the dependencies. Lets discuss one by one

Implicit parameter

This is the way that we have used in our last two post, where we use the same name in parameter which we defined while creating. This is most vulnerable as discussed in the first paragraph as well in case we minify our script files. As we used in one of our example

var myangularapp = angular.module('myangularapp', []);
myangularapp.service('Circle', function () {

    var pi = 3.14;
    this.Area = function (r) {
        return pi * r * r;
    }
    this.Circuimference = function (r) {
        return 2 * pi * r;
    }
});

myangularapp.controller("mycontroller", function ($scope, Circle) {
    $scope.AreaofCircle = Circle.Area(5);
    $scope.CircuimferenceOfCircle = Circle.Circuimference(5);
})

Here we are using Circle as parameter as Circle was used while creating the service. As discussed in the previous post, while resolving the dependencies injector comes into picture and as here there there are two parameters passed $scope and Circle services, injector uses the names  and creates the instance using the provider.

This is simplest way to resolve the dependencies but it breaks if minifiers or obfuscators are used as we know these changes the name of parameters based on there algorithm which later does not match with the providers. But if you are sure that minifiers/obfuscators are not going to be used on production then you can happily use this.

$inject property

This is another way to pass the dependency which does not break in case of minification. In this case even if the name of the parameters gets changed still all the dependencies correctly get resolved. Let’s see how

var myangularapp = angular.module('myangularapp', []);

myangularapp.service('Circle', function () {

    var pi = 3.14;
    this.Area = function (r) {
        return pi * r * r;
    }
    this.Circuimference = function (r) {
        return 2 * pi * r;
    }
});

var mycontroller = function ($scope, customCircle) {
    $scope.AreaofCircle = customCircle.Area(5);
    $scope.CircuimferenceOfCircle = customCircle.Circuimference(5);
};

mycontroller.$inject = ['$scope', 'Circle'];
myangularapp.controller('mycontroller', mycontroller);

Here we can see that we annotate $inject property of the function, here we provide the right name of the services in an array. In controller function we have chosen an arbitrary name (customCircle here) but code still runs fine.

As provider names are assigned as values in the array, no minifier or obfuscator changes the values so it works perfectly fine.

Using Inline Array

This approach is preferable way to resolve the dependencies in the Angular. It is just another way of using array values to provide the service names to appropriate functions but much simpler than above. Let’s take the similar example as above and implement it using inline array

var myangularapp = angular.module('myangularapp', []);

myangularapp.service('Circle', function () {

    var pi = 3.14;
    this.Area = function (r) {
        return pi * r * r;
    }
    this.Circuimference = function (r) {
        return 2 * pi * r;
    }
});

myangularapp.controller("mycontroller", ['$scope','Circle', function ($scope, customCircle) {
    $scope.AreaofCircle = customCircle.Area(5);
    $scope.CircuimferenceOfCircle = customCircle.Circuimference(5);
}]);

Here we can see that we create an inline array then we pass all the dependencies in the array and at end we pass the function where the dependencies need to be passed. Here function takes the input parameter which gets resolved to the dependencies based on the values passed in the inline array. We can pass all the services in similar way. This trick requires less code and easily understandable than the previous one.

Here the key thing to be noted that it orderof the values in the array and parameter name is very important if that gets changed then the right dependencies be initialized to right parameters. Same holds true for second option as well.

Important Note – As it is a common practice to use minifier while putting the code at production and somehow the dependency got resolved via first way then it could be disastrous for your application. In case of big application, where many developers working on same application everybody may use its own choice which could lead issues at production. So to avoid this, Angular provide us a way which makes sure that no body uses implicit injection and that is called Strict Dependency Injection. For this we need add one directive ng-strict-di where we provide ng-app as

<div ng-app=”myangularapp” ng-strict-di>

Hope you all have enjoyed this post including two earlier posts on dependency injection in AngularJS. Do share your feedback.

Cheers
Brij

Next Post ==>

Presented on ASP.NET performance tuning in C# Corner Hyd Chapter Event : 12th July 15

11753645_10153514597568086_9069014579518330181_nHello All,

This was the first when I presented at a community event in Hyderabad. I was pretty excited for the day as I started hearing about this city since my college days due to its significance in IT. And yeah Microsoft’s develop center is also in this city. The day arrived and when I reached the venue, I found that most of the seats were full very early and we had some time to start the event as per schedule. Many kept standing throughout the event as the seats were already occupied completely. Here one point to be noted that we organized this event after long in this city and managed to get a venue which was not very known, also not easily accessible. I wasted more than an hour even when somebody was guiding me for the place on phone. Continue reading

Dependency Injection (DI) in {{AngularJS}} Contd.– Part 17

This post is in continuation of my last post DI on AngularJS. In previous post, we first discussed about DI in details with an example of implementing it in JavaScript. Then we discussed the way AngularJS provides us the opportunity to implement DI  and various options available with it.  We discussed two types Value and Factory in detail. In this post, we will be discussing rest three types and will see how DI takes place behind the scene. I would suggest you to go through the previous post (Dependency Injection (DI) in {{AngularJS}} – Part 16) before continuing further.

Note – This post is a part of series of posts on AngularJS and it is 17th one. Click here to view all the post. Apart from that you can also use the navigation links for next and previous post in each post at the bottom to check all.

Continue reading

Dependency Injection (DI) in {{AngularJS}} – Part 16

Dependency Injection (DI) is one of the hottest terms being used in software development nowadays. Most of the application developers talking about this and try using it in their application. One of the key reasons Agile methodology, as it is used in almost every project in one or other way and DI easily fits into this model and help in achieving its goal. I have been getting many request to write about DI in AngularJS so let’s start exploring that.

Note – This post is a part of series of posts on AngularJS and it is 16th one. Click here to view all the post. Apart from that you can also use the navigation links for next and previous post in each post at the bottom to check all. Continue reading

How to use Angular Filter in Controller, Factory, Service – Part 15

In last two posts, we explored about the Filters and saw that how easily we can create custom filter and use as Angular predefined filters. We have used these filters in views for a single as well as an array of items. And this is a normal scenario but there could be some cases where you need to use these filters in other components like Controllers, Factory, Services etc. I have seen many questions around this on different online forums. So I thought to discuss it in detail.

If you are new to Angular Filter or even if you have some idea about, I will highly recommend you to go through my below two earlier Posts then continue. In this post, I will use the same examples that I used in my previous two posts as well.

Exploring Filters in {{AngularJS}} – Part 13

Exploring Filters in {{AngularJS}} Contd. : Custom Filters – Part 14

Note – This post is a part of series of posts on AngularJS and it is fifteenth one. Click here to view all the post. Apart from that you can also use the navigation links for next and previous post in each post at the bottom to check all. Continue reading