简体   繁体   English

VB6中使用CDO/SMTP/TLS发送邮件smtp.office365.com邮件服务器

[英]Using CDO/SMTP/TLS in VB6 to send email smtp.office365.com mail server

I am searching for days to find out how can I set Office365 SMTP server in my VB6 application.我正在寻找几天来了解如何在我的 VB6 应用程序中设置 Office365 SMTP 服务器。 My code is working properly with port 465 and other mail servers.我的代码在端口 465 和其他邮件服务器上正常工作。 BUT it is not working with port 587 and smtp.office365.com但它不适用于端口 587 和 smtp.office365.com

Is there any way I could have TLS via 587 in VB6?有什么办法可以让我在 VB6 中通过 587 获得 TLS?

Thanks谢谢

With this code, I get to send mail using CDO to Office365.使用此代码,我可以使用 CDO 向 Office365 发送邮件。

    Private Message As CDO.Message
Private Attachment, Expression, Matches, FilenameMatch, i

Sub enviar_mail()

Set Message = New CDO.Message
Message.Subject = "Test Mail"
Message.From = "YourEmail@yourdomain.com"
Message.To = ""
Message.CC = ""
Message.TextBody = "my text body here"

Dim Configuration
Set Configuration = CreateObject("CDO.Configuration")
Configuration.Load -1 ' CDO Source Defaults
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourEmail@yourdomain.com"
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YourPass"
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

Configuration.Fields.Update

Set Message.Configuration = Configuration
Message.Send

End Sub结束子

This code worked for me until a few days ago when we switched ISP's (or maybe something changed coincidentally on the server side).这段代码对我有用,直到几天前我们切换了 ISP(或者可能在服务器端巧合地发生了一些变化)。 If I specify port 587, I get a transport error and have not found the solution for that with VBA.如果我指定端口 587,我会收到一个传输错误,并且没有找到 VBA 的解决方案。 Please let me know if this works for you and if you find a way to use 587. (Port 25 doesn't work for me either, same error.)请让我知道这是否适合您以及您是否找到了使用 587 的方法。(端口 25 对我也不起作用,同样的错误。)

Public Function SMTPSend(vSendTo, vsubject As Variant, vmessage As Variant)
'This works
Set emailObj = CreateObject("CDO.Message")

emailObj.From = "name@myemail.com"
emailObj.To = vSendTo
emailObj.Subject = vsubject
emailObj.TextBody = vmessage
'emailObj.AddAttachment "c:\windows\win.ini"

Set emailConfig = emailObj.configuration


emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
'Must exclude port if specifying SSL
'emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "name@myemail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
emailConfig.Fields.Update

emailObj.Send

If Err.Number = 0 Then SMTPSend = True Else SMTPSend = False

End Function

There seem to be a number of posts on various forums suggesting that the CDO library doesn't work with port 587, but that's not true.各种论坛上似乎有许多帖子暗示 CDO 库不适用于端口 587,但事实并非如此。 I think the real issue is that SMTP services using port 587 are doing so because they require STARTTLS authentication and the http://schemas.microsoft.com/cdo/configuration/smtpusessl config option is specific to SSL, not TLS (I know, I know - not the most accurate description but it'll suffice for now) .我认为真正的问题是使用端口 587 的 SMTP 服务这样做是因为它们需要STARTTLS身份验证,并且http://schemas.microsoft.com/cdo/configuration/smtpusessl配置选项特定于 SSL,而不是 TLS (我知道,我知道 - 不是最准确的描述,但现在就足够了)

Instead, there is another setting - http://schemas.microsoft.com/cdo/configuration/sendtls - that does support TLS, so using this with port 587 works fine:相反,还有另一个设置 - http://schemas.microsoft.com/cdo/configuration/sendtls - 确实支持 TLS,因此将其与端口 587 一起使用可以正常工作:

config.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = 2          '2 = cdoSendUsingPort
config.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = "smtp.example.com"
config.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value = 587
config.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value = 1   '1 = cdoBasic
config.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value = "my_username"
config.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value = "myy_very_secret_password"
config.Fields("http://schemas.microsoft.com/cdo/configuration/sendtls").Value = True
config.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout").Value = 60
config.Fields.Update()

This is what worked for me:这对我有用:

flds("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2          
flds("http://schemas.microsoft.com/cdo/configuration/smtpserver")= "smtp.office365.com"
flds("http://schemas.microsoft.com/cdo/configuration/smtpserverport")= 25
flds("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")= 1   
flds("http://schemas.microsoft.com/cdo/configuration/sendusername")= "name@myemail.com"
flds("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypasword"
flds("http://schemas.microsoft.com/cdo/configuration/smtpusessl")= True
flds("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")= 60

I don't know if you could fix it, but I got it with this:我不知道你是否可以修复它,但我得到了它:

Private Sub Command1_Click()

Dim oSmtp As New EASendMailObjLib.Mail
oSmtp.LicenseCode = "TryIt"

' Set your Hotmail email address
oSmtp.FromAddr = "liveid@hotmail.com"

' Add recipient email address
oSmtp.AddRecipientEx "support@emailarchitect.net", 0

' Set email subject
oSmtp.Subject = "test email from hotmail account"

' Set email body
oSmtp.BodyText = "this is a test email sent from VB 6.0 project with hotmail"

' Hotmail SMTP server address
oSmtp.ServerAddr = "smtp.live.com"

' Hotmail user authentication should use your 
' Hotmail email address as the user name. 
oSmtp.UserName = "liveid@hotmail.com"
oSmtp.Password = "yourpassword"

' Set port to 25, if you want to use 587 port, please change 25 to 587
oSmtp.ServerPort = 25

' detect SSL/TLS connection automatically
oSmtp.SSL_init

MsgBox "start to send email ..."

If oSmtp.SendMail() = 0 Then
    MsgBox "email was sent successfully!"
Else
    MsgBox "failed to send email with the following error:" & oSmtp.GetLastErrDescription()
End If

End Sub结束子

Font: https://www.emailarchitect.net/easendmail/kb/vb.aspx?cat=4字体: https ://www.emailarchitect.net/easeendmail/kb/vb.aspx?cat=4

Remember download the library:记得下载库:

http://easendmail-smtp-component-net-edition.soft112.com/ http://easeendmail-smtp-component-net-edition.soft112.com/

Just Replace Parameters!只需替换参数!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM