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:
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
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: AssemblyCopyright("Copyright © Microsoft 2011")]
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.
The following GUID is for the ID of the typelib if this project is exposed to
Version information for an assembly consists of the following four values:
// Major Version
// Minor Version
// Build Number
You can specify all the values or you can default the Build and Revision
by using the '*' as shown below:
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("firstname.lastname@example.org", mailTo,
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:
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.