Send SMTP eMail using C# or VB.NET in Visual Studio 2012
Visual Studio developers can send free SMTP email within .NET applications using C-Sharp or VB.NET
In this Visual Studio tutorial, I'll share C# and VB.NET codes to send email using SMTP via free SMTP email service of mailgun
If you register mailgun, it will provide you a test sub-domain, an SMTP login user and its password. Developers can use these data in order to send SMTP email from their custom applications free up to 300 email per day. mailgun lists SMTP credentials valid for each of your sandbox domains you get when registered on their portal. Although currently you can send 300 emails per day for each of your sub-domains productive, they suggest to use them in your test or development environments.
Before build your code, register or import the System.Net.Mail .NET framework namespace in your application.
System.Net.Mail enables Microsoft .NET Framework developers to create SmtpClient instance, MailAddress instances for recepients and sender of the email message, and the MailMessage object for the e-mail itself.
If you build using VB.NET then register the above namespaces as follows:
Before you can successfully send email messages from your Visual Studio project, developers should authenticate in the SMTP domain using the credentials.
This will provide you SMTP authenticated in your VS program.
Replace the Login and Password string values with the "Default SMTP Login" and a password for this SMTP login that mailgun will provide you.
For the SMTP credentials, coders can use NetworkCredential object. In order to use NetworkCredential, we should register the System.Net namespace as I mentioned before.
If you set credentials property of the SmtpClient object instance (SmtpClient.Credentials property) before you send the email message, your program will be SMTP authenticated on the SMTP server provided by mailgun.
Please check the below code where I provided sample text and imaginary mail addresses for the sake of this Visual Studio tutorial.
You can revise below C-Sharp code and use it in your .NET programs
Source codes showing how to send eMail via SMTP using C# in Visual Studio 2012
Please note that although any address in "From" field of the email messages can be send using this service, it may cause blocking of your emails later. It is safer to send emails from sub-domain address assigned as a sendbox domain to your account when registered.
And below I copied VB.NET source codes which enables developers to send email from their Visual Studio VB.NET projects. Actually following VB code is a translation of the C# code.
Source codes showin how to send SMTP eMail using VB.NET in Visual Studio 2012