Tag Archives: IL

Sending mails with C#

Before I started programming in C# I used to program in VB.net, and something my program had to do quite often was sending mails.
For example to send the output of something to a certain person, or to send a report to someone etc...

So a while back I picked up C# and realised I haven't sond mails yet, so I tried to set up
a Console Mail application, turns out it's quite similar to VB.net after all...

So first Icreated a console application and added the .Net.Mail namespace by writing this
at the top of my project:

using System.Net.Mail;


By doing this you add the ability to send mails to your application, and you can tell
this has happened by IntelliSense. IntelliSense will have added quite a few
features to your IDE.


This once again proves that IntelliSense is a very usefull tool,  probably one of the main reason why one would opt to use Visual Studio instead
of notepad. But let's leave that aside for now.

To make the program better in use, I used variables for almost everything.
The person you want to send a mail to, the subject and the message ofcourse.

It would be a bit silly to just hardcode this anyway.

Now in this part, nothing indicates we are going to send a mail yet, apart from
the names I gave to my variables.

Oh and by the way, they are all of type string, as everything that gets entered
as "var xx = Console.ReadLine();" gets turned into a string.

Now the part where we are going to set up our mail.

Most of this should speak for itself ofcourse, at the first line we say who we are, who
we want to send a mail to and what the subject and message will be. only our
own name is hardcoded here, as it would be a impossible (well, more or less..)
to use someone else their email adress.

our oClient is basically who we are going to pass our mail trough, I'm using a hotmail
account now so for me that would be smtp.live.com, but if you are using gmail
or yahoo mail it would be another one as listed in the comments in the code.

Then our port, this is just the network port that our mail is going to be send trough,
port 25 would also be avaible with some ISP's, but many of them are now denying
this port because of the malware being spread through it.

Next comes our Credentials, this is how we say who we, as the sender, are. Now I put
"password" there, but obviously you have to enter your own password
there as a string.

Next we say we want to enable Ssl (Secure Socket Layer). and our mail is all set up to be
send off.

At this stage in our code, our program knows everything it has to know to send a mail
out, it's just waiting for our call to it.


So after we add this code, we can run the program and our mail will be sond to the specified address.

After Sending mail, it might look as if your program doesn't respond for just a second, but it is just be patient 🙂


Because I send the message to myself, I get the notice of having a new mail just a few seconds after.


And there we go, our mail has had a safe journey and arrived in our mailbox.

Now if you are a bit geeky, and let's face it I am, you might want to see what this code
is doing behind the scenes. After I started reading C# 4.0 Unleashed (the book is here)
I've gotten really into looking code at another level, the intermediate language
that is. So now to look at what this code does I'm going to use IL DASM (Intermediate
language disassembler), this comes with Visual Studio Professional.


after we open our .exe it will load a treeview in ILDASM, which we can now fully expand.

Let's see what we got here.

First thing we see is where our file is located :

Next we see M A N I F E S T which we can open by double clicking on this.


Now here you can look at what M A N I F E S T looks like in IL, and you might wonder
where all of this information comes from? Well it comes from assembly.cs, a
file that gets automatically generated when you start a new project and gets
stored under properties . And if you open up this file, you'll be able to see
where this information comes from.


Here we will see this information:





General Information about an assembly is controlled through the following

set of attributes. Change these attribute values to modify the information

associated with an assembly.

[assembly: AssemblyTitle("SendMails")]

[assembly: AssemblyDescription("")]

[assembly: AssemblyConfiguration("")]

[assembly: AssemblyCompany("Microsoft")]

[assembly: AssemblyProduct("SendMails")]

[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]

[assembly: AssemblyTrademark("")]

[assembly: AssemblyCulture("")]


Setting ComVisible to false makes the types in this assembly not visible

to COM components.  If you need to access
a type in this assembly from

COM, set the ComVisible attribute to true on that type.

[assembly: ComVisible(false)]


The following GUID is for the ID of the typelib if this project is exposed to

[assembly: Guid("30bdc96b-3e26-4023-9263-dc200b77c7db")]


Version information for an assembly consists of the following four values:


//      Major Version

//      Minor Version

//      Build Number

//      Revision


You can specify all the values or you can default the Build and Revision

by using the '*' as shown below:

[assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("")]

[assembly: AssemblyFileVersion("")]

So you can tell M A N I F E S T doesn't just magically gets all this information.

Now let's open Main: void(string[])


This was also the starting point of our application.

Now by double-clicking on this we can see the IL translation of our code.


(I scratched out my password, that's the red blob)

Honestly I thought the IL code for this would be quite complicated, but it isn't at all.

first it sets some variables of the type string.

The Console pushes some messages on the screen and afterwards we say the string variable
should be filled with whatever we type.

Then we  make a newobj:

newobj     instance void



how we did this in C#:

MailMessage oMail = new MailMessage("meeusdylan@hotmail.com", mailTo,
oSubject, oBodyOfMail);

It is pretty similar, we are passing in strings which are the variables we entered.

Basically, the whole mail sending operation is a bunch of strings that get send trough.

The last part, our oClient.Send(oMail); get's translated into the following:

IL_0089:  callvirt
instance void [System]System.Net.Mail.SmtpClient::Send(class

The whole send operation just gets handles by the System.

That's basically all there is to it in IL.

Digging deeper into C#

Let's talk about IL or intermediate language code. When you run a C# program the code will get ‘transformed’ into IL. IL is coding that looks alot more like native machine code, however there are some differences to be noted.

  • Each instruction represents exactly one fundamental operation
  • Control flow information may not be included in the instruction set
  • Registers can be large to limitless.

This code can be handwritten, but I’m not going in on that aspect of programming right now.
Whenever you execute a program in C# the conversion to IL is an automatic step during execution (the right way of naming this would be compiling, your code get’s compiled into assemblies which are containers for IL code).

To truly understand how a language works you have to dig deeper than what you see in your IDE.
So let’s go ahead and take a step into the dark.

Let’s fire up Visual C# and make a console application.
We’re going to build a simple calculator and examine that code afterwards.









This is what the coding looks like to you, it’s very readable code (assuming you’ve got a knowledge of C#). Yet  this is not at all what your machine get’s to deal with. Once you press that debug button alot is going on that’s not visually represented by Visual Studio. To go and look how this code will look like in IL we’re going to use ILDASP, the intermediate language disassembler.

Load up your program and press CTRL+O and navigate to where you saved your application.



















In this case my application was called ILTranslation.exe but the name doesn’t matter (obviously). Go ahead and double click it and the file will get loaded into ILDASM.

Once you’ve done that just click on all the ‘+’ signs in the treeview you get.










Here you get a view of seperate IL pieces that live inside the program you just created.
As you see the Calculator class that we made is a seperate part inside this treeview with subcategories for the Add function we called in.

Just by taking a swift look at this you should be able to recognize pieces of code that you wrote in your C# project. Now go ahead and double click on the ‘add: int32(int32,int32).
















The code represented here is already alot more usefull to your machine than the code you wrote in C#. Let’s just examine some of this code. The add function contains an int32 a and int32 b. These are the 2 arguments your add function will call in.

IL_0001: ldarg.1 -> here ‘a’ is stored.
IL_0002: ldarg2 -> a and b are stored.
IL_0003: add -> this is where ldarg2 gets calculated (which is a + b)
IL_0004: stloc.0 -> Places a value from the stack into the local variable 0
IL_0005: br.s -> means go to IL_0007 in this case.
IL_0007: Loads the local variable 0 onto the stack.
IL_0008: ret -> returns the result of your calculation (a+b)

And that’s the IL code for the add function in C#. Now if you paid attention you should have noticed that IL stands for intermediate language, so this indicated that compiling code into IL is not the last step code has to go trough before being executed by your machine, but we’ll dig deeper into that in a later post.