I need a code to send emails from a Visual Basic 2010 application using Outlook 2007. I found some codes, but it's use an SMTP server. For my application I need to use Outlook. Any ideas? Thank you!
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.

Please look the below code. I don't have experience on this, but a quick look upon i guess this will suite with your requirement.

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
joyacv2Author Commented:

I got many undeclared variables and objects, any other idea?
Sorry i don't have the required knowledge, but i saw the below link, which also refered in the prevous link that i have sent to you.

However , this is a VBA which is working through excel. I am thinking that you may able to get a start from here.

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

joyacv2Author Commented:

I made some changes to the first code that you provided. Adding reference for Microsoft Outlook library and adding changing some objects in the code and it's works. Thank You!
' Declare module level variables
    Dim mOutlookApp As Microsoft.Office.Interop.Outlook.Application
    Dim mNameSpace As Microsoft.Office.Interop.Outlook.NameSpace
    Dim mFolder As Microsoft.Office.Interop.Outlook.MAPIFolder
    Dim mItem As Microsoft.Office.Interop.Outlook.MailItem
    Dim fSuccess As Boolean

    ' Module contains only 2 methods:  
    '    1) GetOutlook() 
    '    2) SendMessage() 
    Public 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

        mOutlookApp = GetObject("", "Outlook.aplication")

        ' If Outlook is NOT Open, then there will be an error. 
        ' Attempt to open Outlook

        If Err.Number > 0 Then
            mOutlookApp = CreateObject("Outlook.aplication")

            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
        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

    Public Function SendMessage() As Boolean
        ' The SendMessage() function reads user entered values and
        ' actually sends the message.

        On Error Resume Next

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

        strSubject = "test"
        strRecip = ""
        strMsg = "test2"
        strAttachment = ""

        ' Any amount of validation could be done at this point, but
        ' at a minimum, you need to 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

        ' Assume success
        fSuccess = True

        ' Here's where the real Outlook Automation takes place
        If GetOutlook() = True Then
            mItem = mOutlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
            mItem.Subject = strSubject
            mItem.Body = strMsg

            ' This code allows for 1 attachment, but with slight 
            ' modification, you could provide for multiple files.
            If Len(strAttachment) > 0 Then
            End If

        End If

        ' Release resources
        mOutlookApp = Nothing
        mNameSpace = Nothing

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

    End Function

Open in new window

joyacv2Author Commented:
This code works with some changes and adding reference from the Microsoft Outlook library 12.0
This is all your effort and my gratitude to you. well done. :)
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
Visual Basic.NET

From novice to tech pro — start learning today.