Free Information Technology Magazines and eBooks

Wednesday, May 06, 2009

How to Send Email using VB.NET or C#

Email notification is a great tool to inform administrators or users that an important  event happened. Fortunately for VB.NET programmers, Microsoft built the .NET framework full of ready- to-use namespaces such as System.Net.Mail. It is the namespace used to send email if you are using the 2.0 (or higher) .NET Framework. Unlike its predecessor (System.Web.Mail), which was introduced in the 1.0 Framework, System.Net.Mail is not built from the CDO/CDOSYS libraries. Instead it is coded from scratch without any interop so it is not dependent to any other COM libraries.


Requirements to send Email in .NET
1. We need to reference the System.Net.Mail namespace to create and send email messages.
Use it on VB.NET

Imports System.Net.Mail


Or in C#

Using system.Net.Mail

2. We need a relay server to send email through. A relay server is a mail server, or a SMTP server/service, that can handle sending email. System.Net.Mail sends the mail to a relay server, and the relay server is responsible for delivering it to the recipient's mail server.

Sample code of sending plain text using VB.NET


'create the mail message
Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")

'set the content
mail.Subject = "This is an email"
mail.Body = "this is a sample body"

'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)


Sample code of sending plain text using C#
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);


To send an email in HTML format, just add set the isBodyHtml property to true of the MailMessage object. After setting it, you can already include HTML tags inside the Body property.

In VB.NET

'set the content to HTML
mail.IsBodyHtml = true


In C#:

//set the content to HTML
mail.IsBodyHtml = true;


To add an attachment on your email, make use of the Attachements property as shown below.

Email Attachment in VB.NET

Dim file as String = "C:\attachment.dat"
If Not file = "" Then
Dim MsgAttach As New Attachment(file)
mail.Attachments.Add(MsgAttach)
End If


Email Attachment in C#

string file = @"C:\attachment.dat";
if (file.Length>0)
{
Attachment MsgAttach = new Attachment(file);
mail.Attachments.Add(MsgAttach);
}


Basically thats all we need to learn to send email using .NET. I also created a VB.NET class that has all email sending functions.
Here's how you can call the ready-to-use class in your application:

Dim oEmail As New clsEmail
oEmail.SendEmailMessage(From_Email, From_Name, strEmailTo, "Event Notification", BodyMsg, "")


You can download the class from Mediafire



For more .NET tips and tricks, subscribe now

3 comments:

adonis said...

hi Bryan!..

i'm actually new in using VBScript..

and my prof. tasked us to send an email using VBScript..

which bothers me.. if its possible?

if it is..

could you send me a sample code?

here's my email: adonis.emperador@gmail.com

thanks in advance :D

Fryan Valdez said...

@adonis-- yes you can send email using VBScript. All you need is a SMTP service. Here is a sample VBscript that sends email using CDO.

Set obj = CreateObject("CDO.Message")
obj.Subject = "Test Message"
obj.From = "you@fryan0911.com"
obj.To = "me@fryan0911.com"
obj.TextBody = "This is a test message text."
obj.Send

adonis said...

i just finish my project using the codes you gave me...

thanks for the help :D

by the way.. nice camera =P