Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Mail Tutorial in VB.NET

Total Hit ( 3370)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Microsoft has made life more easy by adding an in-built .NET class for SMTP Mail. This article will show you how you can take advantage of System.Web.Mail namespace in VB.Net

Basic Mail Message

Click here to copy the following block
Imports System.Web.Mail

Dim m As New System.Web.Mail.MailMessage()

With m
 .From = "abc@mycompany.com"
 .To = "xyz@yourcompany.com"
 .Subject = "Test email from mycompany"
 .Body = "Hellooooo how are you"
End With

SmtpMail.SmtpServer = "MySMTPServerName" '//This can be your own mail server or ISP SMTP server
SmtpMail.Send(m)

Obviously you will need to change the FROM and TO fields to be something more realistic but this should give you an idea of the basics.

You've probably noticed that I never declared SmtpMail anywhere right? No this is not an omission, this is necessary as the SmtpMail class does not need to have an object created, we can directly call the Send method.

HTML Mail

Yes, the above example was bland! So lets add some spice!

Click here to copy the following block
Imports System.Web.Mail

Dim m As New System.Web.Mail.MailMessage()

With m
 .From = "abc@mycompany.com"
 .To = "xyz@yourcompany.com"
 .Subject = "Test email from mycompany"
 .BodyFormat = MailFormat.Html

 .Body = "<HTML><HEAD></HEAD>" & _
          "<BODY><a href='http://binaryworld.net>Click Here</a> to visit binaryworld.net" & _
          "</BODY></HTML>"
          
 .Priority = MailPriority.High
End With

SmtpMail.SmtpServer = "MySMTPServerName"
SmtpMail.Send(m)

Ok so here you can see that we add a message the same as before except we set the format to be HTML. This allows you to easily send any HTML you like in your Email.

We also set the priority of the mail message to high.

Attachments

Ok so you want attachments right? Well with .NET is really very easy to do:

Click here to copy the following block
Imports System.Web.Mail

Dim m As New System.Web.Mail.MailMessage()

With m
 .From = "abc@mycompany.com"
 .To = "xyz@yourcompany.com"
 .Subject = "Test email from mycompany"
 .Body = "Test Email attachments"
 
 .Attachments.Add(New System.Web.Mail.MailAttachment("c:\test1.txt")) 
 .Attachments.Add(New System.Web.Mail.MailAttachment("c:\test2.txt")) 
End With

SmtpMail.SmtpServer = "MySMTPServerName"
SmtpMail.Send(m)

You can see we are mailing test1.txt and test2.txt files as attachments in this example. Similarly you can send as many attachments as your service provider will allow.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.