使用VBA中CDO.Message从Office 365账户发送邮件时遭遇服务器连接失败问题求助
Fix "The transport failed to connect to the server" with CDO.Message for Office 365 in Access VBA
Let's tackle this issue head-on—you're right that the problem ties directly to TLS configuration and proper SMTP port setup for Office 365, especially with your ISP blocking ports 25 and 465. Here's how to get your code working:
Key Configuration Fixes
Office 365's SMTP service has specific requirements that your original code was missing, even when switching to port 587:
- Port 587 requires STARTTLS encryption, not the direct SSL that port 465 uses. Your code had the
sendtlssetting commented out—this is the critical missing piece. - The
smtpusesslsetting isn't needed for 587, since STARTTLS upgrades a plaintext connection to encrypted after the initial handshake.
Corrected VBA Code
Here's the revised code with all necessary tweaks. I've highlighted the key changes:
Dim objMessage, objConfig, fields Set objMessage = New CDO.Message Set objConfig = New CDO.Configuration Set fields = objConfig.fields With fields .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com" .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587 ' Office 365's recommended STARTTLS port .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 ' Enable basic authentication (required by O365) .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "noreply@domain.ca" .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-app-password-or-account-password" ' Read note below about MFA .Item("http://schemas.microsoft.com/cdo/configuration/sendtls") = True ' Force STARTTLS encryption (critical for port 587) '.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True ' Not needed for 587, comment out .Update End With Set objMessage.Configuration = objConfig With objMessage .subject = "Test Message" .From = "noreply@domain.ca" .To = "noreply@domain.ca" .HTMLBody = "Test Message" End With objMessage.Send ' Clean up objects Set objMessage = Nothing Set objConfig = Nothing Set fields = Nothing
Critical Additional Notes
- Multi-Factor Authentication (MFA): If your
noreply@domain.caaccount has MFA enabled, your regular password won't work here. You'll need to create an App Password in your Microsoft Account settings—this is a special password generated specifically for non-browser apps like CDO.Message. - TLS Version Check: Office 365 requires TLS 1.2 or higher. Make sure your Windows system has TLS 1.2 enabled:
- Open Internet Options → Advanced tab → Scroll to Security section → Ensure "Use TLS 1.2" is checked.
- Tenant Permissions: If this is a business Office 365 account, confirm with your admin that SMTP client access isn't blocked for the
noreply@domain.caaccount. Some organizations restrict this for security reasons. - Firewall/ISP Confirmation: Even if telnet works, double-check that the machine running Access doesn't have a local firewall blocking outbound traffic on port 587.
If you still run into issues, first verify your credentials (or app password) by logging into Outlook Web App with them—this rules out account-level problems.
内容的提问来源于stack exchange,提问作者Thetafinity
相关产品推荐
相关产品推荐

