[FAQ's: OD] How do I send an email using Outlook?
Microsoft Office Outlook must be installed on your system in order to automate it using code.
These are a few code examples (Early Binding) of how to send an email using the Outlook Object Model in VB 6, VB.NET, and C#.
Outlook 2003 And VB 6 Code Example:
VB Code:
Option Explicit
'Add a reference to MS Outlook xx.0 Object Library
Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oEmail As Outlook.MailItem
Set oApp = New Outlook.Application
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
.Subject = "Spam - Meow!!!"
.BodyFormat = olFormatPlain
.Body = "Blah, blah, blah..."
.Importance = olImportanceHigh
.ReadReceiptRequested = True
.Attachments.Add "C:\Cat.bmp", 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
Set oEmail = Nothing
oApp.Quit
Set oApp = Nothing
End Sub
Re: [FAQ's: OD] How do I send an email using Outlook?
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
.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
Re: [FAQ's: OD] How do I send an email using Outlook?
Outlook 2003 And C# 2003 Code Example:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Outlook = Microsoft.Office.Interop.Outlook;
/// using Outlook = Microsoft.Office.Core.Outlook;
namespace Outlook_Email_CS
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private Outlook.Application moApp;
private System.ComponentModel.Container components = null;
public Form1()
{
/// Required for Windows Form Designer support
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
"Windows Form Designer generated code"
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
moApp = new Outlook.Application();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
moApp.Quit();
moApp = null;
}
private void button1_Click(object sender, System.EventArgs e)
{
Outlook.MailItem oEmail = (Outlook.MailItem)moApp.CreateItem(Outlook.OlItemType.olMailItem);
oEmail.Recipients.Add("[email protected]");
oEmail.CC = "[email protected]";
oEmail.Recipients.ResolveAll();
oEmail.Subject = "Spam - Meow!";
oEmail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
oEmail.Body = "Blah, blah, blah...";
oEmail.Importance = Outlook.OlImportance.olImportanceHigh;
oEmail.ReadReceiptRequested = true;
oEmail.Attachments.Add(@"C:\Cat.bmp", Outlook.OlAttachmentType.olByValue);
oEmail.Recipients.ResolveAll();
oEmail.Save();
oEmail.Display(false); /// Show the email message and allow for editing before sending
/// oEmail.Send(); /// You can automatically send the email without displaying it.
}
}
}