Excel VBA Send Email via SMTP: Step-by-Step Guide
In this article
You can send email from Excel VBA using SMTP, but not with Excel alone. VBA has no built-in SMTP client, so you need a helper: either the Windows CDO library (Microsoft Collaboration Data Objects, built into most Windows versions) or a third-party COM component such as a dedicated SMTP library. The CDO route is the most common because it requires no extra purchase or install on most PCs. This guide shows the CDO method, which works with any standard SMTP server that accepts a username and password.
What You'll Need
- Excel with the VBA editor enabled (Developer tab visible).
- A working SMTP server address, port, and login credentials from your email provider or IT department. Common ports are 25, 465, and 587; the correct one depends on the server.
- Permission to send through that server. Many providers block relaying from unknown networks, and corporate servers often require the sending address to match the authenticated account.
- Basic familiarity with the VBA editor: opening it with Alt+F11, inserting a module, and pressing F5 to run code.
Menu labels and dialog wording can differ slightly by Excel version, Windows version, and region. The steps below describe the standard desktop Excel experience; Excel for the web does not run VBA macros.
Step-by-Step
- Open the VBA editor. In desktop Excel, press Alt+F11. If the Developer tab is not visible, enable it through File > Options > Customize Ribbon and tick Developer in the right-hand list.
- Insert a standard module. In the VBA editor, use Insert > Module. Code placed in a worksheet module also works, but a standard module keeps it reusable and easy to find.
- Add the CDO reference (optional but recommended). Use Tools > References and tick “Microsoft CDO for Windows 2000 Library” if it appears. If it does not appear, you can still use late binding, which is the version used below and avoids reference problems on other machines.
- Paste the sending procedure. The core pattern is: create the CDO message object, configure the outgoing server, set the sender, recipient, subject, and body, then call Send. A minimal late-bound example looks like this:
Sub SendMail() Dim msg As Object Set msg = CreateObject("CDO.Message") With msg.Configuration.Fields .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com" .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587 .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "you@example.com" .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword" .Update End With msg.From = "you@example.com" msg.To = "recipient@example.com" msg.Subject = "Test from Excel" msg.TextBody = "This message was sent from Excel VBA." msg.Send Set msg = Nothing End Sub - Replace the placeholder values. Put in your real SMTP host, port, username, password, and email addresses. Keep the sendusing value at 2, which means “send over the network” rather than through a local pickup folder.
- Match SSL to the port. Port 465 almost always requires SSL set to True. Port 587 usually requires SSL or STARTTLS; if your provider documents STARTTLS, CDO handles it when SSL is enabled on port 587 in most configurations, but check your provider's instructions. Port 25 is usually unencrypted and often blocked by ISPs.
- Run the macro. Place the cursor inside the procedure and press F5, or run it from the Macros dialog with Alt+F8. Watch for a confirmation or an error dialog.
- Confirm delivery. Check the recipient inbox and the Sent folder of the account you authenticated as. If the message arrives but the sender looks wrong, the server is rewriting the From address, which is common on shared SMTP services.
- Move credentials out of the code before sharing the workbook. Hard-coded passwords in a macro are visible to anyone who opens the VBA editor. For anything beyond a personal test, store the password in a protected location, prompt for it at runtime with an InputBox, or have IT issue a dedicated sending account with limited rights.
Common Mistakes
- Using the wrong port and SSL combination. This is the single most frequent cause of failure. A timeout on 465 with SSL off, or an immediate rejection on 25, usually means the port and encryption do not match the server's requirements.
- Leaving the From address different from the login. Many servers reject or rewrite messages where the From address is not the authenticated account or an approved alias.
- Forgetting .Update. The configuration fields are not applied until Update is called; without it, CDO uses defaults and the send fails or goes to the wrong server.
- Assuming CDO exists everywhere. It is present on standard Windows desktop installs, but it is not available on macOS Excel, and some locked-down corporate images remove it. On a Mac, use a different approach entirely, such as calling a command-line mail tool or an API.
- Testing with a personal free-mail account. Many consumer providers require app-specific passwords or have disabled basic SMTP authentication, so a correct macro still fails. Check the provider's current SMTP documentation.
- Sending in a loop without limits. A macro that emails every row of a large sheet can trip rate limits or get the account temporarily blocked. Test with one recipient first.
Tips
- Build the body and recipient list from worksheet cells rather than hard-coding them, so the same macro works for different data.
- Wrap the Send call in error handling so a failed send does not leave the user staring at a raw VBA error. A simple On Error GoTo label with a MsgBox showing Err.Description is enough to start.
- Use HTMLBody instead of TextBody when you need formatting, and keep the HTML simple; complex CSS is often stripped by mail clients.
- Add a short delay between messages in bulk sends, and consider batching through your provider's bulk-sending service rather than a personal mailbox.
- Keep a test recipient address in a cell so you can switch between testing and live sending without editing code.
- If your organization uses modern authentication or OAuth, basic SMTP username and password may not be accepted at all; ask IT for the approved sending method rather than trying to force it.
Troubleshooting
Work from the simplest checks outward. First confirm the SMTP host, port, and encryption by sending a test message from a normal mail client using the same account. If that fails too, the problem is the account or server, not the VBA code.
If the client works but VBA does not, check the exact error text. “The transport failed to connect to the server” usually points to a wrong host, port, or blocked outbound connection; try another network, since some ISPs and office firewalls block port 25 and sometimes 587. “The server rejected the sender address” points to a From address that does not match the authenticated account. “The server requires authentication” means the authenticate field or username is wrong. A timeout with no message often means SSL is on for a port that does not use it, or the reverse.
Next, verify the configuration block is actually being applied: confirm .Update is present and that no earlier line errors silently. If you used early binding and the reference is missing on another PC, switch to late binding with CreateObject as shown above. If the workbook was moved from another machine, recheck the port and SSL values rather than assuming they carried over correctly.
If your provider recently changed requirements, such as disabling basic authentication, no amount of code adjustment will fix it; you need the provider's current supported method. Finally, if CDO itself is unavailable, confirm by attempting CreateObject in a fresh module; if that fails, the component is not installed and you need an alternative sending method approved by IT.
Frequently Asked Questions
Can Excel VBA send email without Outlook?
Yes. The CDO method talks directly to an SMTP server and does not require Outlook to be installed or running. Outlook automation is a separate approach that does need Outlook.
Why does my macro work on one PC but not another?
The most common reasons are a missing CDO component, a firewall blocking the outbound port, or a different Excel and Windows version. Check the error text on the failing machine and verify the port and SSL settings there.
Does this work on Excel for Mac?
No. CDO is a Windows component, so the code above does not run on macOS Excel. You need a different sending method on a Mac.
Is it safe to put my password in the macro?
No. Anyone who can open the VBA editor can read it. Use a dedicated sending account with limited rights, prompt for the password at runtime, or store it outside the workbook in a protected location.
Can I send to multiple recipients at once?
Yes. Separate addresses with semicolons or commas in the To field, or loop through a list and send one message per recipient. Test with a small list first to avoid rate limits.
Why does the email arrive with a different sender address?
The SMTP server is rewriting the From header, usually because it does not match the authenticated account or an approved alias. Use the address the server expects, or ask the provider how to authorize the alias.