SENDING EMAILS FROM VISUAL BASIC 2010 USING OUTLOOK

HI,

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!
LVL 1
joyacv2Asked:
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.

MaduKpCommented:
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.

http://www.serverwatch.com/tutorials/article.php/1474711/Send-E-mail-Using-Microsoft-Outlook-Automation.htm

0

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:
Hi,

I got many undeclared variables and objects, any other idea?
0
MaduKpCommented:
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.

http://vbaexcel.eu/vba-macro-code/email-sender-vba-outlook

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

0
Ultimate Tool Kit for Technology Solution Provider

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

joyacv2Author Commented:
Hi,

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
            Err.Clear()
            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 = "test@test.com"
        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.Recipients.Add(strRecip)
            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
                mItem.Attachments.Add(strAttachment)
            End If

            mItem.Save()
            mItem.Send()
        End If

        ' Release resources
        mOutlookApp = Nothing
        mNameSpace = Nothing

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

    End Function

Open in new window

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