-
Apr 22nd, 2006, 02:12 PM
#1
[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
.To = "meow@example.com"
.CC = "ruff@example.com"
.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
Last edited by RobDog888; Aug 23rd, 2006 at 04:05 PM.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Aug 23rd, 2006, 04:05 PM
#2
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
.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
Last edited by RobDog888; Jul 22nd, 2007 at 02:27 PM.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Aug 23rd, 2006, 04:05 PM
#3
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("bgates@microsoft.com");
oEmail.CC = "meow@example.com";
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.
}
}
}
Last edited by RobDog888; Apr 1st, 2007 at 05:43 PM.
Reason: Switched to Code tags for formatting of code.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|