Sending Simple E-Mail in C# - Implementations
(Page 3 of 4 )
The first implementation relies on the System.Web.Mail component that is part of the .NET 1.0 and 1.1 Framework. This is useful for those of you coding in older environments. Check out the attached block of code.
// works only in pre-.NET 2.0. Frameworks (1.0 or 1.1)
. . .
using System.Web.Mail;
. . .
SmtpMail.SmtpServer = "smtp.mail.com";
string from = "jondoe@bla.net";
string to = "justme@isp.com";
string subject = "voila";
string body = "sdfsdfsdf, yes, it's all about sdfsdfsdf.";
SmtpMail.Send(from, to, subject, body);
As you can see at first we specify the component that we’ll use later on. Right after that we need to specify the SmtpServer. This is the address of the SMTP server. Then we also need those four strings of “from address,” “to address,” “subject,” and “body” of the e-mail message that is going to be sent. Once we get all of this prepared, we just call the Send function from the SmtpMail class— voila!
For extensive documentation regarding the capabilities of System.Web.Mail components I strongly and wholeheartedly suggest reading this website.
Now let’s see the second solution. Here we’re going to use the System.Net and System.Net.Mail components, all located within the currently latest frameworks.
// works only in post-.NET 2.0. Frameworks (2.0, 3.5)
. . .
using System.Net;
using System.Net.Mail;
. . .
MailMessage mail = new MailMessage();
mail.From = new MailAddress("jondoe@bla.net");
mail.To.Add("esther@mybusiness.net");
mail.Subject = "voila";
mail.Body = "sdfsdfsdf, yes, it's all about sdfsdfsdf.";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("your_account@gmail.com", "your_password");
try
{
smtp.Send(mail);
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message, "Error: " + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Don’t be fooled by the length of the above block of code. It isn’t harder; it is just a little bit more extensive because I’ve opted for the GMail solution where both SSL encryption and the necessary Network Credentials are to be supplied in order to establish the connection with their SMTP server. Also, error handling isn’t neglected!
On the first lines we specify the components. We create a new MailMessage object called mail. Then we add content to its fields (from, to, subject, body). We head over to add the SMTP server details as well. The port we’re going to use here with GMail’s server is 587. We use EnableSsl to enable SSL (true is the boolean variable). And ultimately, we create a new NetworkCredential using your GMail account information.
Since you are going to do all of this just for yourself to learn and try things out, nobody is going to steal your password or anything. It is safe as long as you do not give out your code, however, you shouldn’t ever commercialize codes from tutorials because they are always way too simplistic and monetizing them is close to impossible. Anyway, play around and see how it works.
The try and catch block will “pick up” if there are any errors and you will get an error message box if that’s the case. You may get something like connection timed out (Wrong port? Slow connection?) or transfer failed (Wrong address? Wrong account information?) and so forth. If the mail goes through all right, you won’t get a message box. Have fun!
Once again, don’t forget to check out this site for extensive documentation on the capabilities, functions, and abilities of System.Net.Mail component. It is worthwhile to look into this because this isn’t obsolete like the earlier System.Web.Mail.
And here comes the final commercial example, using the Chilkat Mail component coming from Chilkat Software. You can download it from here. Keep in mind that you need to send any string through the UnlockComponent method in order to enter into the trial period. If you purchase a license, you will get a valid code that you should pass through the said method. Check out the attached source code.
Chilkat.MailMan mailman = new Chilkat.MailMan();
mailman.UnlockComponent("Unlock 30-day trial period");
mailman.SmtpHost = "smtp.server_goes_here.com";
Chilkat.Email mail = new Chilkat.Email();
mail.From = "Jeff Mike <jeff@mybusiness.com>";
mail.AddTo("Jon Doe", "jondoe@bla.net");
mail.Subject = "voila";
mail.Body = "sdfsdfsdf, yes, it's all about sdfsdfsdf.";
bool success = mailman.SendMail(mail);
if (success)
{
Console.WriteLine("Done.");
}
else
{
Console.WriteLine(mailman.LastErrorText);
}
Let’s explain in short what the code does. We create a new MailMail object called mailman, unlock the component (trial period), add the SMTP server address, and right after that we create the mail Email object. We add the necessary fields such as From, Recipient (AddTo is used because it is added to a collection of recipients!), Subject, and Body. We also create a boolean variable called “success” just for error handling.
Surely, the above code assumes that you have downloaded and extracted the ChilkatDotNet.dll and added it as a reference into your project solution. If you have done all of this, then the code will compile and run without issues. For more practical examples using the Chilkat component just check out their official “example section.”
Next: Final Thoughts >>
More C# Articles
More By Barzan "Tony" Antal