[FAQ's: OD] How do I send an email using Outlook?-VBForums
Results 1 to 3 of 3

Thread: [FAQ's: OD] How do I send an email using Outlook?

  1. #1

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,705

    [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:
    1. Option Explicit
    2. 'Add a reference to MS Outlook xx.0 Object Library
    3. Private Sub Command1_Click()
    4.     Dim oApp As Outlook.Application
    5.     Dim oEmail As Outlook.MailItem
    6.     Set oApp = New Outlook.Application
    7.     Set oEmail = oApp.CreateItem(olMailItem)
    8.     With oEmail
    9.         .To = "meow@example.com"
    10.         .CC = "ruff@example.com"
    11.         .Subject = "Spam - Meow!!!"
    12.         .BodyFormat = olFormatPlain
    13.         .Body = "Blah, blah, blah..."
    14.         .Importance = olImportanceHigh
    15.         .ReadReceiptRequested = True
    16.         .Attachments.Add "C:\Cat.bmp", olByValue
    17.         .Recipients.ResolveAll
    18.         .Save
    19.         .Display 'Show the email message and allow for editing before sending
    20.         '.Send 'You can automatically send the email without displaying it.
    21.     End With
    22.     Set oEmail = Nothing
    23.     oApp.Quit
    24.     Set oApp = Nothing
    25. 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 (VBA, VB 6, VB.NET, C#)
    Senior Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

  2. #2

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,705

    Re: [FAQ's: OD] How do I send an email using Outlook?

    Outlook 2003/2005 And VB.NET 2003 Code Example:
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Add a reference to MS Outlook xx.0 Object Library
    4. Imports Microsoft.Office.Interop
    5. 'Imports Microsoft.Office.Core
    6.  
    7. Public Class Form1
    8.     Inherits System.Windows.Forms.Form
    9.  
    10.     Private moApp As Outlook.Application
    11.     Private mbKillMe As Boolean = True
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         Dim oEmail As Outlook.MailItem
    15.         Me.Cursor = Cursors.WaitCursor
    16.         oEmail = DirectCast(moApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
    17.         With oEmail
    18.             .To = "meow@example.com"
    19.             .CC = "ruff@example.com"
    20.             .Subject = "Spam - Meow!"
    21.             .BodyFormat = Outlook.OlBodyFormat.olFormatPlain
    22.             .Body = "Blah, blah, blah..."
    23.             .Importance = Outlook.OlImportance.olImportanceHigh
    24.             .ReadReceiptRequested = True
    25.             .Attachments.Add("C:\Cat.bmp", Outlook.OlAttachmentType.olByValue)
    26.             .Recipients.ResolveAll()
    27.             .Save()
    28.             .Display() 'Show the email message and allow for editing before sending
    29.             '.Send() 'You can automatically send the email without displaying it.
    30.         End With
    31.         Me.Cursor = Cursors.Default
    32.     End Sub
    33.  
    34.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    35.         Try
    36.             moApp = CType(GetObject(, "Outlook.Application"), Outlook.Application)
    37.             mbKillMe = False
    38.         Catch ex As Exception
    39.             If moApp Is Nothing Then
    40.                 moApp = New Outlook.Application
    41.                 mbKillMe = True
    42.             End If
    43.         End Try
    44.         If moApp Is Nothing Then
    45.             MessageBox.Show("Outlook is not installed or available.", "RobDog888's Outlook FAQ", MessageBoxButtons.OK)
    46.             Me.btnCreate.Enabled = False
    47.             Exit Sub
    48.         End If
    49.        
    50.     End Sub
    51.  
    52.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    53.         If mbKillMe = True Then
    54.             If Not moApp Is Nothing Then
    55.                 moApp.Quit()
    56.                 moApp = Nothing
    57.             End If
    58.         End If
    59.     End Sub
    60.  
    61. 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 (VBA, VB 6, VB.NET, C#)
    Senior Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

  3. #3

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,705

    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 (VBA, VB 6, VB.NET, C#)
    Senior Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.