How to send email through a VB application ?

Hi everybody,

I want to send an email to multi users through my VB application.
I don't know if I can use a VB class for this case or I should download a smtp mail component.
I would be thankful if you can suggest me the best solution.
Please give me a code example for former solution or introduce me an effective free, permanent component.

Thank you in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You can download the smtp.ocx which is freely available on the net. Its damn easy to use it to send mails.


you can get the smtp.ocx from the site below:

or a free download from the site

Also there is sample code on this site which uses MAPI TO SEND EMAIL.

I myself have used this smtp.ocx in an industry application and it works fine.

The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

        I think now everything is shareware. I have a free copy somewhere. Will find out and if you can give me ur mail id will send it to you by tomorrow.

others might have suggest . see you can use the mail in 2 ways 1. use any email client (eg MS Outlook)
2. use MAPI controls
what i would suggest is you configure you mail box in the local pc where the program will run and use Outlook object  in your program.  
The code ..
Dim mOutLookApp As Outlook.Application
Dim mNameSpace As Outlook.NameSpace
Dim mItem As Outlook.MailItem

Set mOutLookApp = New Outlook.Application
Set mNameSpace = mOutLookApp.GetNamespace("MAPI")
mNameSpace.Logon ,, False, True

    Set mItem = mOutLookApp.CreateItem(olMailItem)
    mItem.To = strEmailAddress
    mItem.Subject = "This is for you"
    mItem.HTMLBody = strMessage
    mItem.Sensitivity = olConfidential
    mItem.Importance = olImportanceHigh
yu can change the parameters asper your wish for mNameSpace.Logon as this is the one whihc logs you on to the messaging network
  I Used VBsendMail.dll component and it is working fine.U can get the source code for the dll and for sending Mail(VB) at See the code/ at the bottom of the Page).

Just give the SMTP Id for outgoing u can send mails.U can find a document which contains all properties,Methods etc which will help u.

if u have any doubts u can contact me at
Yes Venkat, I agree :)
pershengAuthor Commented:
everybody thankyou very much.My problem has been solved.
P.S: Dear Venkat thanks for your explanation.
I am very surprised that no one posted a solution that incorporates an MX Lookup - thus removing any necessity on any intermediary SMTP server.

Hands down, even over some commercial solutions - GSMAIL  (free by the war) is the ONLY component that does this MX Lookup AND will push out an email over SMTP.

What does this mean ?  

This means you can send email from your VB app WITHOUT having to specify an SMTP server.

Without doubt you have entertained automated error reporting via email from VB, only to hit the barrier of finding or requesting permission to use clients SMTP server.

Now ?  No more:   As a prototype -  I've included a simple sub called ReportError

I occasionally for one reason or another will have my apps fire off emails, regardless if my client has configured or even HAS an SMTP server.

Code as follows -  to use ?  simply download the GSMAIL componed from here:

Public Sub ReportError()
 Dim Res As Variant 'The result of a method call.
    Dim sDNS, sMX, sSQL As String
    Dim s1, s2, s3, s4 As String

    Dim sContent As String
    On Error GoTo 0
    sDNS = GetDNSinfo
    sDNS = Trim(sDNS)
    sMX = Mid(sMX, InStr(1, sMX, "@") + 1)

    'Open the SMTP session.
    Res = SMTP.Open(sMX)
    sContent = "Error #" & lErrorNumber & " in Procedure" & sProcedure & "." & vbCrLf
    sContent = sContent & "Description: " & sErrorDescription & vbCrLf
  '  sContent = sContent & "User Comments:" & Text1.Text & vbCrLf
    sContent = sContent & "Build: " & App.Major & "." & App.Minor
    'Send the email.
    s1 = ""
    s2 = ""
    s3 = "Error #" & lErrorNumber
    s4 = sContent
    Res = SMTP.Send(s1, s2, s3, s4)
bSkipError = False
End Sub

Again, note, no specification of SMTP server :)  

Tim Miltz
Chief Technology Officer
Etaion Software
hi , in this case you must let your PC to be a server than install an freeware call WmailTo. it got the feature
 SMTP (email) sender
 NNTP (news) poster
 Multiple receivers of emails, grouped as "to", "cc" and "bcc". "Bcc" (blind carbon copy) receivers will not be listed in the message header and is therefore conveniant for mass-mailing of newsletters and information.
 Support for crossposting to several newsgroups when used as a news poster.
 Feature to insert one or several text files to the message body.
 Feature to include one or several binary/text files as binary attachments (using MIME 1.0 multipart/base64 encoding). The encoded message format is the current "standard" and can be read by all modern mail readers.
 Feature to turn off MIME encoding to handle old mail systems or special tasks.
 Feature to convert DOS codepage (european) character sets to the Windows OEM charset. This allows you to use national caharcters in a DOS window, and have these converted to the Windows chracter set by the program. This feature is very conveniant if you need to send the output from some DOS program throug email.
 Easy to configure SMTP host and personal information - have some fun with your buddies by sending them a letter from the president or something similar (note - this actually is illegal, so you should only do this with people you know well, and _be there_ when they get the mail).
 Full source code in c++ is included
 so u can download it.

or you can go to to find your solution.
Dear All

No Need to use a third party component for sending a mail.Need to install IIS and the mail can be send by simple code in VB
else you can use the MAPI control in this manner

MAPISession1.NewSession = False
MAPISession1.LogonUI = True


MAPIMessages1.SessionID = MAPISession1.SessionID

MAPIMessages1.RecipAddress = ""

'MAPISession1.MsgReceiptRequested = True
MAPIMessages1.MsgSubject = "MIS Schedular mails"    

MAPIMessages1.MsgNoteText = "test"


try to use Mapi32.dll
U can use WinSock tool to achieve solution. But u have to customize o your own..
You could use the object in CDO.

Scott Wiseman
<advertizing removed per http:help.jsp#hi106>
u have to options:-
1)use MAPI
 in this first case u need a valid user account in outlook ,it isi following code:-
Private Sub Command1_Click()
MAPISession1.DownLoadMail = False
MAPISession1.UserName = "user"
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.MsgSubject = Text4.Text
MAPIMessages1.MsgNoteText = RichTextBox1.Text
MAPIMessages1.RecipAddress = Text1.Text
End Sub

2)use winsock control.
  in this case u have to handle all the things from sockets to sevrer etc.
 so i recommend use mapi.u can code a simple program according to ur needs.

kind reagards
-hardik shah
I did something similiar to this using this script
I use
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Success = ShellExecute(0&, vbNullString, "", vbNullString, "C:\", 1)

And works fine,
This is the fully .NET way to do it.

Imports System.Web.Mail

Dim mailMessage as MailMessage = new MailMessage
mailMessage.From = ""
mailMessage.To = "
mailMessage.Subject = "Hello World"
mailMessage.BodyFormat = System.Web.Mail.MailFormat.Text
mailMessage.Attachments.Add(New MailAttachment(pathToAttach))
mailMessage.Body = "Hello World"
SmtpMail.SmtpServer = "smtpServer" // your smtp server's address

You could download a SMTP dos Facility. And make a VB script to Write a Batch file and run it.
Hi  thedude112286

I am using the same with adding the reference of system.web dll
but i m facing 1 error can u please guide me !

An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll

Additional information: Could not access 'CDO.Message' object.

Hi Tim Miltz,

That component you refer to 'GSMail' is no longer available at the URL you suggested.
Would you have a copy I could try or suggest somewhere to get it from ?


Sorry for the delay, I just got back from vacation.  This faq is dedicated entirely to your problem.
Hi, I am trying to send email through a .net app without using local SMTP server. I also looked at a java program to do the same thing. But both programs seems to need a host and port to connect. How do I get the SMTP server of the recepient?
Hi dbdoshi,

Try using a well known ISP's SMTPserver

For example: I use

    You are asking how to find the recipients Mail Exchange Server, for this you need to do an MX Lookup.   JUST as you would do an A Record lookup to see what the web server is for a given domain, e.g.  (,  you perform an MX lookup against the DNS server to see what the SMTP server associated with such a domain is registered to, so,  to find the web IP for, you do an A record lookup, to find the email server associated with, you do an MX Lookup.

   Just stip off the domain from the email address, and either via sockets, or some high level component, as I recommended above (the ONLY one I know of that does both the MX lookup FOR YOU, AND the direct send via sockets) GMail from Goldenseal studios, free of charge for non commercial use.  To be found here->

   Many mail servers though will do a check back against the senders address/IP to see if it HAS an MX Lookup associated with it, or some means to try to validate it's not coming from some spam spewer, as you can imagine, direct send is REALLY what is happening with any mail server - at the end of the day so to speak.  What's happening is, you are establishing a connection, following SMTP protocol, over TCP/IP - you shake hands with the server, let it know you've got something, ask if they exist, and drop it off, all through very special reserved words set in stone 20 + years ago, when the standard was invoked.

  The component above has been extremely useful for me in automated emailings from applications I've built and NOT wanted to rely on the user having a default mail server configured, as, I myself don't and only use web based email, no thanks to MS VBS in outlook, and other dangers.  

   If you are looking to quietly send data from a clients instance of your application you've distributed though (of course, you need to specify what you're doing in the EULA or - well - get sued),  I recommend XMLHTTP coupled with ASP/ on some dedicated server just living to catch requests/posts for this, or some similar server based processing model for incoming requests/posts.   I have found that clients firewalls don't even trigger using this approach, and again, I can't stress enough, make SURE you have that in the EULA, myself ? I'm nice, I bring up a box if for error reporting stating,  Send, Do Not Send, as Microsoft does, if used for error reporting.

   Hope this more than answered your question, not sure if this is still open, I don't need any more points, heh, I'm just happy to help you understand the basics here of MX Lookup - the Mail Exchange record associated with a domain that is set for the last stop DNS server responsible for such a domain, AND the ability to do a lookup, by stripping domain off of email address, AND the simplicity and beuty of GMail from GoldenSeal, which, technically, is a real nice mail server by definition, free, lightweight, AND via COM, abstracts ALL of the SMTP jargon you'd otherwise have to master, JUST to send an email through code WITHOUT having to specify an SMTP server on the clients machine to send from.

Tim Miltz
Senior... Senior... Senio... something or another...
Etaion Software

Tim, thank you very much in taking time to reply. I figured out how to do it myself after searching quite a bit. The problem was there was not one reference (atleast that I could find) which explained what you explained. So had to hunt quite a bit to get my program going. I wish there was an explanation like yours about a month ago. !!
I can help u writing the solution using C# and u can translate it to VB..

add new linkLabel to ur form ... go to its Tag property and write the following:

 double click on the linkLabel then write:

thats it .. good luck
hi! i've tried the shell command to send email.. is there anyway that i can send emails without opening it, i mean i want my program to just automatically send the mail, one that i dont have to click "send" to be be able to send it. Coz what my program does is open a mail browser, i still have to click the send button to be able to send it. I prefer shell command if possible. thanks.
I use the following VB script to in my login script to send an email. It points to the SMTP server. No third party software is needed.

Does anyone know how to insert the computername, time and/or date in the message body?
Set objEmail = CreateObject("CDO.Message")
objEmail.From = ""
objEmail.To = ""
objEmail.Subject = "Subject Text" 
objEmail.Textbody = "Text body." 
objEmail.Configuration.Fields.Item _
    ("") = 2
objEmail.Configuration.Fields.Item _
    ("") = _
        "SMTP server name" 
objEmail.Configuration.Fields.Item _
    ("") = 25

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.