Outlook 2003/2005 And VB.NET 2003 Code Example:
VB Code:
Option Explicit On
Option Strict On
'Add a reference to MS Outlook xx.0 Object Library
Imports Microsoft.Office.Interop
'Imports Microsoft.Office.Core
Public Class Form1
Inherits System.Windows.Forms.Form
Private moApp As Outlook.Application
Private mbKillMe As Boolean = True
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oEmail As Outlook.MailItem
Me.Cursor = Cursors.WaitCursor
oEmail = DirectCast(moApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
With oEmail
.To = "meow@example.com"
.CC = "ruff@example.com"
.Subject = "Spam - Meow!"
.BodyFormat = Outlook.OlBodyFormat.olFormatPlain
.Body = "Blah, blah, blah..."
.Importance = Outlook.OlImportance.olImportanceHigh
.ReadReceiptRequested = True
.Attachments.Add("C:\Cat.bmp", Outlook.OlAttachmentType.olByValue)
.Recipients.ResolveAll()
.Save()
.Display() 'Show the email message and allow for editing before sending
'.Send() 'You can automatically send the email without displaying it.
End With
Me.Cursor = Cursors.Default
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
moApp = CType(GetObject(, "Outlook.Application"), Outlook.Application)
mbKillMe = False
Catch ex As Exception
If moApp Is Nothing Then
moApp = New Outlook.Application
mbKillMe = True
End If
End Try
If moApp Is Nothing Then
MessageBox.Show("Outlook is not installed or available.", "RobDog888's Outlook FAQ", MessageBoxButtons.OK)
Me.btnCreate.Enabled = False
Exit Sub
End If
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If mbKillMe = True Then
If Not moApp Is Nothing Then
moApp.Quit()
moApp = Nothing
End If
End If
End Sub
End Class