Narasimha
Narasimha

Reputation: 3766

How to send email via vb.net catch exception with outlook programmatically?

I am working on vb.net service application and application hosting in to the server running daily, sometimes service is stopped because exception is raising.

How to send email via vb.net issue (String) with outlook programmatically?

Upvotes: 2

Views: 5381

Answers (2)

skynetsysadmin
skynetsysadmin

Reputation: 78

I use this to send via outlook. Just reference the Microsoft Outlook DLL

    Private Shared Sub SendMail(pMessage As String)

        Dim objMissing As Object = Type.Missing
        Dim objOutlook As Outlook.Application = Nothing
        Dim objNS As Outlook.NameSpace = Nothing
        Dim objMail As Outlook.MailItem = Nothing

        Try
            ' Start Microsoft Outlook and log on with your profile.
            ' Create an Outlook application.
            objOutlook = New Outlook.Application()

            ' Get the namespace
            objNS = objOutlook.GetNamespace("MAPI")

            ' Log on by using a dialog box to choose the profile.
            objNS.Logon(objMissing, objMissing, False, False)

            ' create an email message
            objMail = CType(objOutlook.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)

            ' Set the properties of the email.
            With objMail
                .Subject = "Subject Line"
                .To = "[email protected]"
                .Body = pMessage
                .Display(True)
            End With

        Catch ex As Exception

        Finally
            ' Manually clean up the explicit unmanaged Outlook COM resources by  
            ' calling Marshal.FinalReleaseComObject on all accessor objects. 
            ' See http://support.microsoft.com/kb/317109.

            If Not objMail Is Nothing Then
                Marshal.FinalReleaseComObject(objMail)
                objMail = Nothing

            End If

            If Not objNS Is Nothing Then
                Marshal.FinalReleaseComObject(objNS)
                objNS = Nothing

            End If
            If Not objOutlook Is Nothing Then
                Marshal.FinalReleaseComObject(objOutlook)
                objOutlook = Nothing

            End If

        End Try

    End Sub

MSDN has a lot of info on this as well.

http://msdn.microsoft.com/en-us/library/office/ff865816.aspx

Upvotes: 2

Steve
Steve

Reputation: 5545

First, you should cath your errors, not let your service crash. Second, you can use the System.Net.Mail namspace to send mail. You will want to create a MailMessage and send it through a SMTPClient. Each will give you examples.

Upvotes: 1

Related Questions