You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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 sendtls setting commented out—this is the critical missing piece.
  • The smtpusessl setting 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

  1. Multi-Factor Authentication (MFA): If your noreply@domain.ca account 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.
  2. 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.
  3. 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.ca account. Some organizations restrict this for security reasons.
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:02:46