INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Visual Basic (Microsoft) Versions 5/6 FAQ

E-Mail

How to send Mail From VB With or Without Using Outlook by Vince
Posted: 10 Aug 00

This FAQ has been written in response to multiple questions being asked as to how to send mail from VB


With OutLook

You must select the Outlook object from your VB Project references

Public sub SendMail(tSubject As String, tBody As String)

'***********************
'Description: Uses the outlook object to create and send a mail using the passed parameters of tsubject,tbody and tTo
'***********************

Dim oItem As Outlook.MailItem
Dim oitems As Items
Set oOutlook = New Outlook.Application

Set oitems = objInbox.Items

Set oItem = oOutlook.CreateItem(olMailItem)
'
With oItem

.To = tTo
.Body = tBody
.Subject = tSubject
.send

End With


Without Oulook (My preferred method)

Reference the CDO 1.21 object from your references section in the VB IDE


Private Sub SendMail(tBody As String, tSubject As String)

Dim objSession As MAPI.Session
Dim objmessage As MAPI.Message
Dim objRecipient As MAPI.Recipient
Dim objAttach As MAPI.Attachment


'Create the Session Object
  Set objSession = CreateObject("mapi.session")
  
 
  
  
  'Logon using the session object
  'Specify a valid profile name if you want to
  'Avoid the logon dialog box
  'If you don't include a profilename then a dialog is popup requesting one
  objSession.Logon profileName:="MS Exchange Settings"


  'Add a new message object to the OutBox
  Set objmessage = objSession.Outbox.Messages.Add

  'Set the properties of the message object
  objmessage.Subject = tSubject
  objmessage.Text = tBody
  'Popup the global addresslist to select your recipients
  'Force the resoluion of the named recipients
  Set objmessage.Recipients = objSession.AddressBook(, "Select Recipients", , True, , ">>")
  
  'To add attachments
  Set objAttach = objmessage.Attachments.Add
        objAttach.Name = "testing" 'Pass your own name
        objAttach.Source = "C:\Boot.ini" 'Pass in your own filename
        objAttach.Type = CdoFileData 'This is the Default
  
  

  'Send the message
  objmessage.Send showDialog:=False
  
  
  'Logoff using the session object
  objSession.Logoff

End Sub

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close