Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Send mail from VB application Using Outlook 2000

Dear experts,

I know it is very simple to do but somehow i could not do it. poor guy. Please help me to write that function how to use Outlook object and send mails automatically. I tried to do it, after seting the recepeint, suject and body, VB was indicating some error on send command. some amboguitiy.. .. no idea la...

Thanking you for your prompt support....

waiting.....
0
mamaj
Asked:
mamaj
  • 2
1 Solution
 
gencrossCommented:
Here is some code that will open Outlook and mail with attachments

Sub Email()
   
   Dim objOL As Object
   Dim objEMail As Object
   
   Set objOL = CreateObject("Outlook.Application") 'opens Outlook
   
   Set objEMail = objOL.CreateItem(olMailItem) ' opens new email
   With objEMail
       .Recipients.Add "me@me.com" 'recipient's email address
       .Subject = "Your details " & x 'subject of the email
       .Body = "Details"
       'To send an attachment
       .Attachments.Add ("C:\my documents\file.txt")
       .Send
   End With
   
   Set objEMail = Nothing
       
End Sub

This should do it if I did not forget anything.
0
 
rpaiCommented:
You could use the following lines of code to send an email using Outlook 2000
-------------------Start Code------------------------------
Public Function SendMessage() As Boolean

On Error Resume Next

Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String

strSubject = "Test Email"
strRecip = "test@test.com"
strMsg = "Test Email using Outlook Application"
'strAttachment = "C:\test.txt"

'-- verify that the user supplied an Email address for a recipient.
If Len(strRecip) = 0 Then
    strMsg = "You must designate a recipient."
    MsgBox strMsg, vbExclamation, "Error"
    Exit Function
End If

'-- Success
fSuccess = True

'-- The Outlook Automation
If GetOutlook = True Then
    Set mItem = mOutlookApp.CreateItem(olMailItem)
    mItem.Recipients.Add strRecip
    mItem.Subject = strSubject
    mItem.Body = strMsg
   
'-- Allows 1 attachment, but with slight modification, you could provide for multiple files.
    If Len(strAttachment) > 0 Then
        mItem.Attachments.Add strAttachment
    End If
   
    mItem.Save
    mItem.Send
End If
----------------------End Code-----------------------------
' Release resources
Set mOutlookApp = Nothing
Set mNameSpace = Nothing

If Err.Number > 0 Then fSuccess = False
SendMessage = fSuccess

End Function

Private Function GetOutlook() As Boolean
' The GetOutlook() function sets the Outlook Application
' and Namespase objects and opens MS Outlook
On Error Resume Next

' Assume success
fSuccess = True

Set mOutlookApp = GetObject("", "Outlook.application")

' If Outlook is NOT Open, then there will be an error.
' Attempt to open Outlook
If Err.Number > 0 Then
    Err.Clear
    Set mOutlookApp = CreateObject("Outlook.application")
       
    If Err.Number > 0 Then
        MsgBox "Could not create Outlook object", vbCritical
        fSuccess = False
        Exit Function
    End If
End If

' If we've made it this far, we have an Outlook App Object
' Now, set the NameSpace object to MAPI Namespace
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")
   
If Err.Number > 0 Then
    MsgBox "Could not create NameSpace object", vbCritical
    fSuccess = False
    Exit Function
End If

' Return the Success Flag as the value of GetOutlook()
GetOutlook = fSuccess
   
End Function
0
 
rpaiCommented:
Ooops..!
The ---------End Code ----------- came up pre-maturely in my above post!
0
 
Éric MoreauSenior .Net ConsultantCommented:
I prefer a lot using vbSendMail. Very easy to use. Free. Does not have requirements.

http://www.freevbcode.com/ShowCode.Asp?ID=109
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now