Office 365 and Classic ASP vs. VB.net SMTP Setting

2019-07-19 13:13发布

问题:

There are several questions about classic ASP and Office 365, but none that seem to answer my particular scenario, so here goes.

I set up an email account on Office 365 and am trying to do an SMTP test with the following code:

Dim ObjSendMail, mailSubject, mailBody
Set ObjSendMail = CreateObject("CDO.Message")
mailSubject = "Test"
mailBody = "Test"
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 240
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "email@domain.com"
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
ObjSendMail.Configuration.Fields.Update

ObjSendMail.To = "to_email@anotherdomain.com"
ObjSendMail.Subject = mailSubject
ObjSendMail.From = """From Name"" <email@domain.com>"
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = mailBody
ObjSendMail.Send

This generates the following error:

CDO.Message.1 error '80040213'

The transport failed to connect to the server.

I've figured out that it's due to the smtpusessl setting, but I need that, and if I turn it off I get error '8004020e'.

Now, here's the weird thing...I can do the SMTP test successfully using VB.net and the code below:

    Dim mailClient As New SmtpClient("smtp.office365.com")
    mailClient.Port = 587
    mailClient.EnableSsl = True
    'Your network credentials are going to be the Office 365 email address and the password
    Dim cred As New System.Net.NetworkCredential("email@domain.com", "password")
    mailClient.Credentials = cred
    Dim message As New MailMessage()
    message.From = New MailAddress("email@domain.com", "From Name")
    message.[To].Add("to_email@anotherdomain.com")
    message.Subject = "Test Office 365 SMTP"
    message.Body = "Test Body"
    mailClient.Send(message)

This tells me that I shouldn't need to configure anything on my server to get the classic ASP version to work...which is what the site I'm working on is built in. The question I have is...what stupid setting am I missing that will get rid of the transport error?

Thanks.

回答1:

Try port 25 instead of 587. All of your other settings, including smtpusessl, can remain the same.

I suspect the problem with port 587 is that CDO.Message doesn't support the protocol required (TLS? MSA?). I found a few other internet posts stating the same problem with ASP/VBScript and port 587 but that .NET applications did not have this problem.