在vb脚本中使用cdo配置发送邮件时出现问题

时间:2014-02-04 10:49:41

标签: excel vba vbscript smtp

我要求我需要从excel发送邮件。我们使用MS Excel来执行某些功能,它将在执行所有步骤后生成报告。

文件夹结构如下。

A =>(report.html,A1,A2,A3)  {0}是A(主文件夹)中的文件夹,每个文件夹中都有report.html。     主报告,基本上包含所有其他报告的链接。用户可以查看子报告。

直到这个,它运作良好。

下一个要求是压缩文件夹A,以便该zip文件中的所有其他文件夹都可用。这也有效。

最后,我们需要将zip文件发送到给定的邮件ID。这是问题所在。

我正在附加我用来发送邮件的vbs文件。但它在不同的网络上表现不同。

在办公室网络上:CDO.Message.1:传输无法连接到服务器。

在客户端网络上(使用Citrix):CDO.Message.1:传输无法连接到服务器。

在家庭网络上:它有效,我收到了邮件。

SMTP设置 服务器 - smtp.office365.com 港口 - 587(也试过25) SSL - true(根据Office 365,他们使用TSL)

如何识别问题?

以下代码。

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "Example CDO Message" 
objMessage.From = "$MailID$" 
objMessage.To = "$ToID$" 
objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "$SMTPServer$"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "$MailID$"

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "$Password$"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $PortNo$ 

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10000

'objMessage.Addattachment "D:\Vn\POC\attachfile.txt"

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send

1 个答案:

答案 0 :(得分:0)

这是一种权限案例,您可以访问SMTP网络中的home服务器,但不能访问officeclient网络。如果它在您的home网络上完美运行,那么我希望它具有这种性质。因此,如果您需要从办公室运行此功能,我会与您的IT管理员联系,以了解授予哪个组访问SMTP服务器的权限。

从编码的角度来看,你的代码是否使用了所有CONST声明?我可以看到5个声明,但只有1个(2见下文)使用

这行代码不是= 2,而是= cdoNTLM

objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

相关问题