how to send email?

suxintee used Ask the Experts™
hi. i would wan to know how to send email using visual basic. full source code will be most welcomed. if anyone who know how to do 2 way messaging(SMS). i need ur help badly. thanks. if u have source code, mail me at or dun worry, iwill grade u. cheer~
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Here is an example using Outlook, customize the code as neccessary:

Private Sub Command4_Click()
   Dim iOutlook As Outlook.Application
   Dim myApp As Outlook.MailItem
   Set iOutlook = New Outlook.Application
   Set myApp = iOutlook.CreateItem(olMailItem)
   temp = "ryancys"
   txtMessage = "Hello World Again!"
   AttachFile = "C:\Documents and Settings\Administrator\My Documents\My Pictures\next.gif"
   myApp.To = ""
   myApp.Subject = "Hello World!"
   myApp.Attachments.Add AttachFile
   myApp.Body = "Hi " & temp & vbCrLf & vbCrLf & txtMessage
   'myApp.HTMLBody = ""
   'Do While iOutlook.GetNamespace("MAPI").GetDefaultFolder(olFolderOutbox).Items.Count <> 0
   '    DoEvents
   MsgBox "Successfully Sent", vbInformation, "Email Sent"
End Sub

Alternatively See use tools: 

hi friend i am also doing the same thing suggested by ryan but i am getting some run time error as : Some tme error comes that the item has been deleted or moved ...and error code is -214735267.....
Second error is "Sent method of _DsentItems failed..

Coding is:-
Dim objOutlook As New Outlook.Application
Dim objOutlookMsg As Outlook.MailItem

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    MsgBox rs(1)
    objOutlookMsg.To = rs(1)
    objOutlookMsg.Subject = "test"
    objOutlookMsg.Body = Text1.Text
    'Dim i As Integer
    'For i = 0 To Me.lslfilename.ListCount - 1
    '    objOutlookMsg.Attachments.Add Me.lslfilename.List(i)
    Set objOutlookMsg = Nothing


ALTHOUGH THE MAILS ARE GOING BUT THIS ERROR IS COMING....Time being i have put "ON ERROR RESUME NEXT" but i don't want to do that...plz tell me what to do!!!!!!!!!!!

I use vbSendMail for sending email. It does not depend on outlook, CDO or MAPI - only Winsock.

pg_india- if you have a question, ask it in the topic. Don't leech off someone else :-)

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Dirk HaestProject manager

Create a class module (clsOutlook)

Option Explicit

Private outlook As New outlook.Application
Private mblnTerminateOutlook As Boolean

Private Sub Class_Initialize()
 mblnTerminateOutlook = outlook.ActiveExplorer Is Nothing
End Sub

Private Sub Class_Terminate()
 If mblnTerminateOutlook Then outlook.Quit
 Set outlook = Nothing
End Sub

Public Sub SendMail(strTo As String, strSubject As String, strMessage As String, strCc As String, strBcc As String, Optional vntAttachmentPath As Variant)
   Dim i As Integer
   Dim str As String

   With outlook.CreateItem(olMailItem)
       .To = strTo
       .Subject = strSubject
       .Body = strMessage & vbCrLf & vbCrLf
       If strCc <> Empty Then .CC = strCc
       If strBcc <> Empty Then .BCC = strBcc
       If IsArray(vntAttachmentPath) Then
           For i = 0 To UBound(vntAttachmentPath)
               .Attachments.Add vntAttachmentPath(i), , Len(.Body)
           Next i
       End If
'        If you want to save the email as file
'        str = ("c:\" & strTo & "-" & strSubject & ".msg")
'        .SaveAs str
   End With
End Sub

On your form where you want to send your email:
' Declaration
Private mOutlook As New clsOutlook

(replace to,subject,message,cc,bcc,attach to valid strings)

' send mail versturen with attachments
mOutlook.SendMail To, Subject, Message, CC, bcc, attaach

'mail without attachments
mOutlook.SendMail To, Subject, Message, CC, bcc

Try CDONTS library

dim odont
dim omapi
dim eFrom
dim eTo
dim econtext
dim etext

efrom = ""
eTo = ""
econtext = "sample text..."
etext= "sample......."

set odont = createobject ("CDONTS.NewMail")
set omapi = createobject ("MAPI.Session")

odont.send efrom,eto,econtext,etext

set odont = nothing

msgbox "ok"

This program sends a email using the Microsoft Mapi Controls 6.0.
U can try this.

Private Sub cmdReceive_Click()
    MS.NewSession = True
    MS.UserName = mack
    MS.Password = Form1.Tag
    MS.Action = 1
    MM.SessionID = MS.SessionID
    MM.FetchUnreadOnly = True
    txtSubject.Text = MM.MsgSubject
    txtMessage = MM.MsgNoteText
    MS.Action = 2
End Sub
Private Sub cmdSend_Click()
    MS.NewSession = True
    MS.Action = 1
    MM.SessionID = MS.SessionID
    MM.MsgSubject = txtSubject.Text
    MM.MsgNoteText = txtMessage
    'MM.MsgOrigAddress = ""
    MM.RecipAddress = txtTo.Text
    txtMessage.Text = ""
    MS.Action = 2
End Sub


hi guys..thanks for ur help..i will try the methods and come back to this...really appreciate ur help!


anyone who has source code to send to me? better than copy and paste and create icons again in  visual basic....thanks a lot~
You have to do something yourself suxintee, you are not paying anyone here to do work for you. You ask for help with a problem and get suggestions on how to fix it.

We all gave you different source code, and each is good for different reasons. You have to decide which is best for you and your situation.

If you really need a complete solution, try offering more points and maybe someone will spend more time on you. Otherwise, please understand that the experts here are not payed employees of EE and cannot do others' work as well as thier own.



ok! i think i found something i want from thanks a lot from everybodY!


thanks for the website anyway.
Ryan ChongSoftware Team Lead

Weird, same link as what i have posted, but not me deserve the points..
Shucks ryancys, I didn't see you posted that. I kinda skipped over your post after noticing the type of code you used.)
I asked CS to transfer the answer to you.

You're off to a pretty weak start here. First of all, you selected the wrong comment as an answer; I can fix that.

Second of all, you gave the grade a C, even though you said you used the suggested code. Grades here aren't like school, and even if they were, that one would be intolerable. You should grade and assume an A, unless you have a reason not to (such as you had to do more research, or had to figure something out from the code), in which case a B might be okay.

Instead of giving a C, you should ALWAYS give the Experts a chance to raise the grade; they're willing to help you if you'll tell them the results aren't satisfactory. After all, it's your question -- you don't have to close it until you're satisfied.

ryancys: points for you at

Monchanger: so it does not reflect on your grading history, I've changed the grade here.

CS Moderator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial