Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Option Compare Database
' instance attribute email components
Private sSubject As String
Private cRecip As New Collection
Private cBCC As New Collection
Private sBody As String
Private cFiles As New Collection
Private cItems As New Collection
Private cItemTypes As New Collection
Private bDispEmail As Boolean
Private Sub Class_Initialize()
Disp = False
End Sub
' Get Display Email Property
Public Property Get DispEmail() As Boolean
DispEmail = bDispEmail
End Property
' Set Display Email Property
Public Property Let DispEmail(aValue As Boolean)
bDispEmail = aValue
End Property
' Get Subject Property
Public Property Get Subject() As String
Subject = sSubject
End Property
' Set Subject Property
Public Property Let Subject(aValue As String)
sSubject = aValue
End Property
' Get Body Property
Public Property Get Body() As String
Body = sBody
End Property
' Set Body Property
Public Property Let Body(aValue As String)
sBody = aValue
End Property
' Get Recipients collection
Private Property Get Recip() As Collection
Set Recip = cRecip
End Property
' Get BCC collection
Private Property Get BCC() As Collection
Set BCC = cBCC
End Property
' Get file attachmets collection
Private Property Get Files() As Collection
Set Files = cFiles
End Property
' Get image attachmets collection
Private Property Get Items() As Collection
Set Items = cItems
End Property
' Get image attachments types collection
Private Property Get ItemTypes() As Collection
Set ItemTypes = cItemTypes
End Property
' Add To recipient to collection
Public Sub addRecip(Rep As String)
cRecip.Add (Rep)
End Sub
' Add BCC recipient to collection
Public Sub addBCC(Rep As String)
cBCC.Add (Rep)
End Sub
' Add File to collection
Public Sub addFile(file As String)
cFiles.Add (file)
End Sub
' Add item and type to collections
Public Sub addItem(i As String, t As String)
cItems.Add (i)
cItemTypes.Add (t)
End Sub
' Send email
Public Sub Send()
On Error GoTo Email_Error
Dim oApp As Outlook.Application
Dim l_Msg As MailItem
Dim colAttach As Outlook.Attachments
Dim l_Attach As Outlook.Attachment
Dim strEntryID As String
Dim x As Integer
' CDO objects
Dim oSession As MAPI.Session
Dim oMsg As MAPI.Message
Dim oAttachs As MAPI.Attachments
Dim oAttach As MAPI.Attachment
Dim colFields As MAPI.Fields
Dim oField As MAPI.Field
Dim oReceipt As Outlook.Recipient
' create new Outlook MailItem
Set oApp = CreateObject("Outlook.Application")
Set l_Msg = oApp.CreateItem(olMailItem)
With l_Msg
'add recipients
For Each eml In Recip
Set oReceipt = .Recipients.Add(eml)
oReceipt.Type = olTo
Next
'add BCC recipients
For Each eml In BCC
Set oReceipt = .Recipients.Add(eml)
oReceipt.Type = olBCC
Next
'set email subject
.Subject = Subject
'set body HTML
.HTMLBody = Body
End With
'clear recipients
Set oReceipt = Nothing
'check items for mime encoding
If Items.Count > 0 Then
' attach items
Set colAttach = l_Msg.Attachments
For x = 1 To Items.Count
Set l_Attach = colAttach.Add(Items.Item(x))
Next
' clear attachments
Set colAttach = Nothing
Set l_Attach = Nothing
' close and save mail item
l_Msg.Close olSave
' get MSG ID
strEntryID = l_Msg.EntryID
' clear mail item
Set l_Msg = Nothing
' initialize CDO session
Set oSession = CreateObject("MAPI.Session")
oSession.Logon "", "", True, False
' get the message created earlier
Set oMsg = oSession.GetMessage(strEntryID)
' Set attachments
Set oAttachs = oMsg.Attachments
' loop and set items as mime attachments
For x = 1 To Items.Count
Set oAttach = oAttachs.Item(x)
Set colFields = oAttach.Fields
Set oField = colFields.Add(CdoPR_ATTACH_MIME_TAG, ItemTypes.Item(x))
Set oField = colFields.Add(&H3712001E, "item" & x)
oMsg.Fields.Add "{0820060000000000C000000000000046}0x8514", 11, True
oMsg.Update
Next x
' clean up CDO objects
Set oField = Nothing
Set colFields = Nothing
Set oMsg = Nothing
oSession.Logoff
Set oSession = Nothing
Set oAttach = Nothing
Set oAttachs = Nothing
' get the Outlook MailItem again
Set l_Msg = oApp.GetNamespace("MAPI").GetItemFromID(strEntryID)
End If
' add normal file attachments
For Each att In Files
l_Msg.Attachments.Add (att)
Next
' save email
l_Msg.Save
' check if display or send
If DispEmail Then
l_Msg.Display
Else
l_Msg.Send
End If
' clean up email object
Set l_Msg = Nothing
Set oAttach = Nothing
Set oApp = Nothing
Exit Sub
Email_Error:
MsgBox err.Description, vbOKOnly + vbCritical, "Error: " & err.Number
End Sub
' Declare and initialise EmailWrapper Object
Dim anEmail As New EmailWrapper
' Add inline MIME attachments
anEmail.addItem "c:\MyDirectory\CSS\myCSS.css", "text/css"
anEmail.addItem "c:\MyDirectory\Images\Logo.gif", "image/gif"
' Add Subject
anEmail.Subject = "This is my email subject"
' Add HTML Body
anEmail.Body = "<html><head><title>This is the HTML page title.</title>" & _
"<link href=""cid:item1"" rel=""stylesheet"" type=""text/css"" />" & _
"</head><body><img id=""logo"" src=""cid:item2"" title=""This is my Logo"" alt=""This is the alternative text"" />" & _
"<p>This is just some paragraph text</p></body></html>"
' Add standard attachments
anEmail.addFile "c:\MyDirectory\Docs\myWordDoc.doc"
' Add Recipients
anEmail.addRecip "fred@mydomain.com"
anEmail.addRecip "bob@mydomain.com"
' Add BCC recipients
anEmail.addBCC "boss@mydomain.com"
anEmail.addBCC "accounts@mydomain.com"
' Send Email
anEmail.Send
' Clear object
Set anEmail = Nothing
' Add inline MIME attachments
anEmail.addItem "c:\MyDirectory\Images\header.gif", "image/gif"
anEmail.addItem "c:\MyDirectory\Images\side.gif", "image/gif"
anEmail.addItem "c:\MyDirectory\Images\people.gif", "image/gif"
' get HTML template
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("C:\MyDirectory\eNews\myFile.html, 1)
sHTML = f.readall
' replace holders for images
sHTML = Replace(sHTML, "header.gif", "cid:item1")
sHTML = Replace(sHTML, "side.gif", "cid:item2")
sHTML = Replace(sHTML, "people.gif", "cid:item3")
anEmail.Body = sHTML
Option Explicit
' instance attribute email components
Private sSubject As String
Private cRecip As New Collection
Private cBCC As New Collection
Private sBody As String
Private cFiles As New Collection
Private cItems As New Collection
Private cItemTypes As New Collection
Private bDispEmail As Boolean
Private sSender As String
Const olMailItem As Integer = 0
Const olTo As Integer = 1
Const olCC As Integer = 2
Const olBCC As Integer = 3
' constructor
Private Sub Class_Initialize()
bDispEmail = False
End Sub
' Get Display Email Property
Public Property Get DispEmail() As Boolean
DispEmail = bDispEmail
End Property
' Set Display Email Property
Public Property Let DispEmail(aValue As Boolean)
bDispEmail = aValue
End Property
' Get Subject Property
Public Property Get Subject() As String
Subject = sSubject
End Property
' Set Subject Property
Public Property Let Subject(aValue As String)
sSubject = aValue
End Property
' Get Body Property
Public Property Get Body() As String
Body = sBody
End Property
' Set Body Property
Public Property Let Body(aValue As String)
sBody = aValue
End Property
' Get Recipients collection
Private Property Get Recip() As Collection
Set Recip = cRecip
End Property
' Get BCC collection
Private Property Get BCC() As Collection
Set BCC = cBCC
End Property
' Get file attachments collection
Private Property Get Files() As Collection
Set Files = cFiles
End Property
' Get item attachments collection
Private Property Get Items() As Collection
Set Items = cItems
End Property
' Get item attachmet types collection
Private Property Get ItemTypes() As Collection
Set ItemTypes = cItemTypes
End Property
' Add To recipient to collection
Public Sub addRecip(Rep As String)
cRecip.Add (Rep)
End Sub
' Add BCC recipient to collection
Public Sub addBCC(Rep As String)
cBCC.Add (Rep)
End Sub
' Add File to collection
Public Sub addFile(file As String)
cFiles.Add (file)
End Sub
' Add item and type to collections
Public Sub addItem(i As String, t As String)
cItems.Add (i)
cItemTypes.Add (t)
End Sub
' Get Sender Property
Public Property Get Sender() As String
Sender = sSender
End Property
' Set Sender Property
Public Property Let Sender(aValue As String)
sSender = aValue
End Property
' Send email
Public Sub Send()
On Error GoTo Email_Error
Dim oApp As Object
Dim l_Msg As Object
Dim colAttach As Object
Dim l_Attach As Object
Dim strEntryID As String
Dim x As Integer
Dim oReceipt As Object
Dim oPA As Object
Dim eml As Variant
Dim att As Variant
Const PR_ATTACH_MIME_TAG = "http://schemas.microsoft.com/mapi/proptag/0x370E001E"
Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
Const PR_ATTACHMENT_HIDDEN = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"
' create new Outlook MailItem
Set oApp = CreateObject("Outlook.Application")
Set l_Msg = oApp.CreateItem(olMailItem)
With l_Msg
'add recipients
For Each eml In Recip
Set oReceipt = .Recipients.Add(eml)
oReceipt.Type = olTo
Next
'add BCC recipients
For Each eml In BCC
Set oReceipt = .Recipients.Add(eml)
oReceipt.Type = olBCC
Next
'set email subject
.Subject = Subject
'set body HTML
.HTMLBody = Body
' check for sender address
If Nz(Sender, "") <> "" Then
.SentOnBehalfOfName = Sender
End If
End With
'clear recipients
Set oReceipt = Nothing
'check items for mime encoding
If Items.Count > 0 Then
' attach items
Set colAttach = l_Msg.Attachments
For x = 1 To Items.Count
Set l_Attach = colAttach.Add(Items.Item(x))
Set oPA = l_Attach.PropertyAccessor
oPA.SetProperty PR_ATTACH_MIME_TAG, ItemTypes.Item(x)
oPA.SetProperty PR_ATTACH_CONTENT_ID, "item" & x
oPA.SetProperty PR_ATTACHMENT_HIDDEN, True
Next
End If
l_Msg.Save
' add normal file attachments
For Each att In Files
l_Msg.Attachments.Add (att)
Next
' save email
l_Msg.Save
' check if display or send
If DispEmail Then
l_Msg.Display
Else
l_Msg.Send
End If
Exit_Email:
' clean up email object
Set l_Msg = Nothing
Set l_Attach = Nothing
Set oApp = Nothing
Set oPA = Nothing
Exit Sub
Email_Error:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume Exit_Email
End Sub
anEmail.DispEmail = True
anEmail.Sender = "another@mydomain.com"