Last Update: 2018 - 06 - 02
Creating Emails with a mailto-Hyperlink from Access
by Philipp Stiefel, originally published January 15th, 2016
The integrated SendObject-method of the DoCmd-Object (DoCmd.SendObject) is already a very simple way to send email from a Microsoft Access application. Its requirements are pretty low with only a MAPI compatible mail client required. - However there might be times where even that is too much already.
No MAPI with the Windows 10 Mail App
I just had to deal with the case of a user who had a pretty plain installation of Windows 10. His computer has no Microsoft Office installation, just the Microsoft Access Runtime is installed and no other mail client than the Windows 10 Mail application is available. – To my surprise, the Windows 10 Mail App seems not to support MAPI.
If you execute a DoCmd.SendObject command with only the Windows 10 Mail App installed, you will get two error messages like these:
Well, I should not have been too surprised, as Microsoft is for some while warning that “The use of Simple MAPI is discouraged. It may be altered or unavailable in subsequent versions of Windows.” on its Simple MAPI introduction page in Windows Dev Center.
Ok, so what now?
The mailto protocol
There is another way to send an email from Access, which is rarely used because it is quite simple but also very limited. The mailto-Hyperlink is a very generic method to generate (not send!) an email message. It is defined in the RFC2368 specification and is usually used on webpages to provide an easy way for the reader of the page to create an email.
Sure, your Access application is not going to run in a web browser that handles hyperlinks all the time. But there is the FollowHyperlink method available right in Access VBA. You can use this method to invoke any hyperlink as long there is an application registered on your computer to handle that protocol. So if there is any mail application installed that is linked to the mailto URL scheme, you can use the Application.FollowHyperlink method to invoke a mailto link.
This line of VBA code should start you mail client and have it create a new email message to that address.
Setting subject, body and additional recipients
The mailto Hyperlink is quite commonly used with a single recipients email address and sometimes the subject. But it is possible as well to set the body of the email message with a mailto link.
Here is a sample mailto link to create an email with a predefined subject and body.
But don’t go overboard with this. The possible total length of the mailto link is limited. I was not able to find a limit for the link length in any specification. But my own research indicates that the maximum total length of any URL address that is passed to Application.FollowHyperlink may not exceed 2074 characters. Longer texts resulted in the rather uninformative “run-time error ‘87’: An unexpected error has occurred.”
While I was not able to find any reliable information on this on the internet, there still might be additional, lower limits for the maximum length of the message body with other mail clients. If there is actually a limit with Outlook 2013 and the Windows 10 Mail App, it is higher than the limit on the total length of the URL set by the FollowHyperlink-Method.
In same way as Subject and Body, you can use the CC and BCC headers to set additional recipients of your generated email.
URL encoding body and subject
You’ll probably notice that the body part of the link in the sample above is URL-encoded. URL encoding (sometimes also called percent encoding) is a very simple way to encode characters that would convey a special meaning within an URL. – Remember: the target of the mailto link is basically an URL.
To URL encode a text you need to replace all special characters with the hexadecimal value of their ASCII Code, preceded by a percent symbol (hence the name).
To use the mailto link to set the body of the email, we need a function to URL encode the text of the email we want to send. It’s not difficult to write such a function. It took me less than ten minutes to write this:
Public Function URLEncode(ByVal strInput As String) As String Const CHARS_TO_ENCODE As String = "%_!_#_$_&_'_(_)_*_+_,_/_:_;_=_?_@_[_]_ _" & vbCr & "_" & vbLf Dim charsToEncode As Variant Dim i As Long Dim retVal As String retVal = strInput charsToEncode = Split(CHARS_TO_ENCODE, "_") For i = LBound(charsToEncode) To UBound(charsToEncode) retVal = Replace(retVal, charsToEncode(i), "%" & Right("0" & Hex(Asc(charsToEncode(i))), 2)) Next i URLEncode = retVal End Function
There might be more efficient ways to write a similar function, but I think my implementation is fairly easy to understand as far as I can see it works reliably.
Assuming you copied the above function to your application and have got a simple form in with three text box controls txtRecipient, txtSubject and txtText, and a btnEmail button, you can generate an email message with these lines of code as event handler of the email button.
Private Sub btnEmail_Click() Dim recipient As String Dim subject As String Dim body As String recipient = Nz(Me.txtRecipient.Value, "") subject = URLEncode(Nz(Me.txtSubject.Value, "")) body = URLEncode(Nz(Me.txtText.Value, "")) Application.FollowHyperlink "MailTo:" & recipient & "?subject=" & subject & "&body=" & body End Sub
I have not tested the URL encode function for performance. As the text length is limited anyway and you are probably not calling this function thousands of times in a send-email-context, I think performance is fairly irrelevant here.
(Not) adding attachments
The official specification of the mailto link does not define a way to add attachments to an email message. Nevertheless some email clients (e.g. Outlook 98 – 2003) support using a virtual attachment-header to add an attachment to an email.
You can see how it could work in this example.
You should absolutely not rely on this to work. Even if does work now in a certain configuration, it might stop to work tomorrow because of an update to the mail client.
No sending of messages
For security reasons the specification of the mailto URL scheme explicitly disallows the mail client to send the email directly without further interaction with the user.
So you can use a mailto link to create an email message, but then the user will have to hit the send button himself. So always keep in mind that your program can never know for sure, if an email was actually sent or not.
Advantages and disadvantages
I wrap up this topic with a short list of the advantages and the limitations/disadvantages of this method to send email.
Other options to send email from Access applications
If you haven’t done so already, I strongly suggest you take a look at the other options to send emails from Access before you decide which suits your requirements best.
© 1999 - 2018 by Philipp Stiefel - Privacy Policiy