无法使用vbs脚本从smtp.gmail.com的587端口发送邮件

4

我正在尝试使用vbs脚本发送邮件,但无法成功。我正在使用服务器smtp.gmail.com和端口587。奇怪的是当我将端口更改为25时,它可以正常工作。以下是我使用的代码:

    SMTPMail "to", "cc", "TEST", "TEST"


Function SMTPMail(ByVal sTo, ByVal sCc, ByVal sSubject, ByVal sBody)


    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

    Dim objMessage

    set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = sSubject
    objMessage.Sender = "sender"
    objMessage.From = "from"
    objMessage.To = sTo
    objMessage.CC = sCc
    objMessage.TextBody = sBody


    '==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") = "smtp.gmail.com"    

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

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

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

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

    '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") = 60

    objMessage.Configuration.Fields.Update()
    objMessage.Send()

End Function

Thank you in advance.

1个回答

10

Gmail用户可以通过官方网站或使用第一方或第三方应用程序和服务来访问他们的帐户。其中第一方应用程序是例如Google Android的官方Gmail应用程序,而Thunderbird和Windows 8的邮件客户端应用程序则是第三方应用程序。

2014年4月,Google 宣布将改进其服务的登录安全性,并影响向该公司发送用户名和密码的任何应用程序。

当时,该公司建议切换到OAuth 2.0,但直到现在才强制执行。

如果您在Google的安全设置下打开新的不太安全应用程序页面,则会注意到默认情况下Google已禁用访问。

注意:只有当您未使用Google应用程序或已为帐户启用双重身份验证时,才会看到该页面。

您可以在此处切换以重新启用不太安全的应用程序,从而恢复访问权限。

enter image description here

另外一个端口是465而不是587,你可以尝试使用这个vbscript,它在我的电脑上使用了端口465

EmailSubject = "Sending Email by CDO"
EmailBody = "This is the body of a message sent via" & vbCRLF & _
        "a CDO.Message object using SMTP authentication ,with port 465."

Const EmailFrom = "self@gmail.com"
Const EmailFromName = "My Very Own Name"
Const EmailTo = "someone@destination.com"
Const SMTPServer = "smtp.gmail.com"
Const SMTPLogon = "self@gmail.com"
Const SMTPPassword = "gMaIlPaSsWoRd"
Const SMTPSSL = True
Const SMTPPort = 465

Const cdoSendUsingPickup = 1    'Send message using local SMTP service pickup directory.
Const cdoSendUsingPort = 2  'Send the message using SMTP over TCP/IP networking.

Const cdoAnonymous = 0  ' No authentication
Const cdoBasic = 1  ' BASIC clear text authentication
Const cdoNTLM = 2   ' NTLM, Microsoft proprietary authentication

' First, create the message

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = EmailSubject
objMessage.From = """" & EmailFromName & """ <" & EmailFrom & ">"
objMessage.To = EmailTo
objMessage.TextBody = EmailBody

' Second, configure the server

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

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

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

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

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

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

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

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

objMessage.Configuration.Fields.Update
'Now send the message!
On Error Resume Next
objMessage.Send

If Err.Number <> 0 Then
    MsgBox Err.Description,16,"Error Sending Mail"
Else 
    MsgBox "Mail was successfully sent !",64,"Information"
End If

1
谢谢您的回答,但我已经在C#中使用了相同的凭据(端口587),它可以正常工作。我想知道为什么从C#可以工作,但从vsb脚本不能。 - user3815821

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接