SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers


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.

using System.Net.Mail; // C-Sharp
using System.Net;
Code

If you build using VB.NET then register the above namespaces as follows:

Imports System.Net.Mail
Imports System.Net
Code

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.

NetworkCredential basicCredential = new NetworkCredential("Login", "Password");
client.UseDefaultCredentials = false;
client.Credentials = basicCredential;
Code

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

SmtpClient client = new SmtpClient();
client.Host = "smtp.mailgun.org";
client.Port = 25; // Mailgun SMTP servers listen on ports 25, 587 and 465

NetworkCredential basicCredential = new NetworkCredential("SMTPLoginByMailGun", "Password");
client.UseDefaultCredentials = false;
client.Credentials = basicCredential;

MailAddress from = new MailAddress("kodyaz@testsandbox.mailgun.org", "Kodyaz");
MailAddress to = new MailAddress("kodyaz@recepient.com"); // recepient
MailMessage message = new MailMessage(from, to);

message.Subject = "C-Sharp Tutorial: Send Email using SMTP";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Body = "Here is a test email sent from C# application built using Visual Studio 2012";
message.BodyEncoding = System.Text.Encoding.UTF8;

client.Send(message);
Code

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.

send email using SMTP in Visual Studio C-Sharp code

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.

Dim client As New SmtpClient()

client.Host = "smtp.mailgun.org"
client.Port = 25

Dim basicCredential As New NetworkCredential("SMTPLoginByMailGun", "Password")

client.UseDefaultCredentials = False
client.Credentials = basicCredential

Dim MailFrom As New MailAddress("kodyaz@testsandbox.mailgun.org", "Kodyaz")
Dim MailTo As New MailAddress("kodyaz@recepient.com")

Dim message As New MailMessage(MailFrom, MailTo)

message.Subject = "VB.NET Tutorial: Send Email using SMTP"
message.SubjectEncoding = System.Text.Encoding.UTF8
message.Body = "Here is a test email sent from Vb.NET program built using Visual Studio 2012"
message.BodyEncoding = System.Text.Encoding.UTF8

client.Send(message)
Code

Source codes showin how to send SMTP eMail using VB.NET in Visual Studio 2012



Visual Studio


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.