在Excel VBA中使用CDO发送电子邮件不起作用 [英] Use CDO Send Emails in Excel VBA Not Working

查看:424
本文介绍了在Excel VBA中使用CDO发送电子邮件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Excel VBA中发送电子邮件.根据对互联网的研究,我启用了Microsoft CDO for Windows 2000 Library并编写了以下VBA代码:

I am trying to send email within Excel VBA. Based on my research on Internet, I enabled Microsoft CDO for Windows 2000 Library and composed my VBA codes as below:

Sub SendCDOMail()
    Dim objCDOMsg As Object

    Set objCDOMsg = CreateObject("CDO.Message")

    'CDO Configuration
    With objCDOMsg.Configuration.Fields
        '
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        'Server port (typically 25, 587)
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
        'SMTP server IP or Name
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
        'Type of authentication, NONE, Basic (Base64 encoded), NTLM
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        'SMTP Account User ID
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx@bbbb.com"
        'SMTP Account Password
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxxx"
        'Use SSL for the connection (False or True)
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
        .Update
    End With

    'CDO Message
    objCDOMsg.Subject = "TEST Subject"
    objCDOMsg.From = "xx@xx.com"
    objCDOMsg.To = "xx@xx.com"
    objCDOMsg.TextBody = "TEST Body"

    objCDOMsg.Send

End Sub

但是,我收到错误消息:

However, I received the error saying:

The server rejected the sender address. The server response was: 530.5.7.57 SMTP;
Client was not authenticated to send anonymous mail during MAIL FROM

我还使用PowerShell尝试了相同的配置,并且可以正常工作(smtpserver,用户名,密码,服务器端口...). 我不知道哪里可能出问题.

I also tried the same configurations using PowerShell and it worked (smtpserver, username, password, server port...). I don't know where it might go wrong.

推荐答案

在这里是:

Here it is: https://technet.microsoft.com/en-us/library/mt210446(v=exchg.150).aspx - note that this is in the Office365 section for "How to set up a multifunction device or application to send email using Office 365,", so there may be other things in there for you too.

这表明您正在连接到SMTP客户端提交终结点(smtp.office365.com),该终结点不能用于直接发送.对于直接发送,使用Office 365租户的MX终结点,该终结点以"mail.protection.outlook.com"结尾.您可以按照直接发送的完整配置说明.

This indicates that you are connecting to the SMTP client submission endpoint (smtp.office365.com), which can't be used for direct send. For direct send, use the MX endpoint for your Office 365 tenant, which ends with "mail.protection.outlook.com." You can find your MX endpoint by following the steps in Full configuration instructions for direct send.

这篇关于在Excel VBA中使用CDO发送电子邮件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆