README Send Email with Attachments #
Overview #
Sends an email with one or more file attachments via the Graph API POST /me/sendMail endpoint. Files are read from disk, base64-encoded using ADODB.Stream and MSXML2.DOMDocument, and embedded directly in the JSON payload as a fileAttachment array. Authentication is via OAuth2 with MFA support using VBAuth.dll.
This approach requires no upload session and no additional scope beyond Mail.Send — provided individual files do not exceed 3 MB and the total payload does not exceed 10 MB. See the section on large files below for the alternative approach when those limits are exceeded.
File size limits #
Per-file limit: 3 MB (3,145,728 bytes) Files must be base64-encoded before embedding. Base64 encoding increases the size of each file by approximately 33%. The 3 MB raw file size limit ensures the encoded attachment remains within what the Graph API accepts in a single POST body without requiring an upload session. The form rejects any individual file exceeding this limit at the point of selection.
Total limit: 10 MB (10,485,760 bytes) across all attachments This is a conservative limit chosen to keep the overall JSON payload at a manageable size for a demo system. The form accumulates file sizes as attachments are added and rejects any addition that would push the total over this limit.
Note: these are raw file sizes. The actual JSON payload will be larger due to base64 encoding and the email headers and body. For production use, consider tightening the per-file limit further if large recipients mailboxes are a concern.
Large files — what to do when limits are exceeded #
For individual files larger than 3 MB, the Graph API provides an upload session mechanism. This approach is more complex but supports files up to 150 MB. It requires the Mail.ReadWrite delegated scope in addition to Mail.Send, and proceeds as follows:
- Create a draft message — POST to
/me/messagesto create a draft and obtain adraftId. RequiresMail.ReadWrite. - Create an upload session — POST to
/me/messages/{draftId}/attachments/createUploadSessionwith the attachment metadata (name, size, content type). Returns anuploadUrl. - Upload the file in chunks — PUT the file bytes to the
uploadUrlin sequential chunks (recommended chunk size: 4 MB). Each PUT includes aContent-Rangeheader. No authentication header is needed on the PUT — the upload URL is pre-authorised. - Send the draft — POST to
/me/messages/{draftId}/sendonce all chunks are uploaded.
This approach is documented at: https://learn.microsoft.com/en-us/graph/sdks/large-file-upload
The additional scope required (Mail.ReadWrite) is broader than Mail.Send alone. In a production implementation this would be the only scope used, as Mail.ReadWrite is a superset. For the demo database, the upload session approach is not implemented — the 10 MB total limit covers all realistic demo scenarios.
Office apps #
- Access (tested M365)
- Excel (untested M365)
Required components #
- VBAuth.dll v1.x
- modVBAuthGlobalConstants.bas (from /shared)
- modSendEmailWithAttachments.bas (this module)
Azure Entra App Registration #
Delegated permissions:
- Mail.Send
This AppID (20) is shared with the HTML Email tab. One app registration and one lclVBAuthAdmin record serve both tabs.
VBAuth Admin Table – Required Values #
| Column Name | Required Value / Example | Notes |
|---|---|---|
| AppID | 20 | Identifies this feature’s configuration record — shared with HTML Email tab |
| ActionName | VBAuth – Send Email (Attachments and HTML) | Display name — any value acceptable |
| ClientID | (your App Client ID) | GUID from Azure App Registration Overview blade |
| TenantID | (your Tenant ID) | GUID from Azure App Registration Overview blade |
| ObjectID | (your Object ID) | Set by PopulateMissingObjectIDs |
| ScopeClaims | Mail.Send | Comma-separated Graph scopes |
| Verb | POST | HTTP method |
| URLPart1 | https://graph.microsoft.com/v1.0/me/sendMail | Graph endpoint |
| URLParameter1 | NO VALUE REQUIRED | Not used |
| URLPart2 | NO VALUE REQUIRED | Not used |
| URLParameter2 | NO VALUE REQUIRED | Not used |
| URLPart3 | NO VALUE REQUIRED | Not used |
| WaitForLogin | 60 | Seconds — NB MSXML2 timeout parameters are in milliseconds |
Module: modSendEmailWithAttachments.bas #
Public constants #
Public Const MAX_FILE_BYTES As Long = 3145728 ' 3 MB per file
Public Const MAX_TOTAL_BYTES As Long = 10485760 ' 10 MB total
Code language: PHP (php)
Public functions #
Public Function FileToBase64(ByVal strFilePath As String) As String
Code language: JavaScript (javascript)
Reads a file from disk using ADODB.Stream (binary mode) and encodes it to base64 using MSXML2.DOMDocument. Returns the base64 string with line breaks removed. Returns “” if the file cannot be read.
Public Function SendEmailWithAttachments( _
ByVal strTo As String, _
ByVal strCC As String, _
ByVal strBCC As String, _
ByVal strSubject As String, _
ByVal strBody As String, _
ByVal blnSaveToSentItems As Boolean, _
ByRef arrFiles() As String, _
ByRef strMessage As String) As Boolean
Code language: JavaScript (javascript)
Sets AppID=20, acquires a bearer token, base64-encodes each file in arrFiles, builds the JSON payload including the attachments array, and POSTs to /me/sendMail. arrFiles is a 1-based String array of full file paths. Size validation must be performed by the caller before calling this function. Returns True on HTTP 202.
Private functions #
JsonEscape(strValue)— escapes special characters for JSON string embedding.BuildRecipientArray(strAddresses)— converts comma/semicolon address lists to Graph recipient array JSON.GetMimeType(strFilePath)— returns a MIME type string for common file extensions, falling back toapplication/octet-stream.BuildAttachmentsJson(arrFiles())— constructs the Graphattachmentsarray JSON fragment, callingFileToBase64for each file.BuildSendWithAttachmentsPayload(...)— builds the complete JSON payload including the attachments array.
Behaviour #
- Runs as the signed-in user (delegated permissions only).
- HTTP 202 Accepted is the success code.
- Files are encoded at send time — large files may cause a brief pause while encoding.
- The
@odata.typefor each attachment is#microsoft.graph.fileAttachment. - MIME types are inferred from the file extension. Unknown extensions use
application/octet-stream. - Authentication is interactive on first run; subsequent calls within the session reuse the cached token.
Notes #
Mail.Sendis the minimum scope — noMail.ReadWriteis needed for files under 3 MB.- Admin consent is required before users can authenticate.
- For files over 3 MB, the upload session approach is required — see the large files section above.
- The 10 MB total limit is a demo-appropriate conservative limit. Production implementations may allow higher totals, subject to Exchange Online mailbox message size limits (typically 35 MB by default, configurable up to 150 MB).
VBAuth, developed and maintained by Andrew Richards (The IT Service Ltd) and Peter Bryant (Corylus Business Systems), © 2025–2026. All rights reserved. https://vbauth.com