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

时间:2016-05-30 16:18:47

标签: email vb6 office365 smtpclient tls1.2

我正在寻找几天来了解如何在VB6应用程序中设置Office365 SMTP服务器。我的代码与端口465和其他邮件服务器一起正常工作。 但它不适用于端口587和smtp.office365.com

我有什么方法可以在VB6中通过587获得TLS?

由于

4 个答案:

答案 0 :(得分:9)

使用此代码,我可以使用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

答案 1 :(得分:0)

这个代码对我有用,直到几天前我们切换ISP(或者可能在服务器端巧合地改变了)。如果我指定端口587,我收到传输错误,并且没有找到VBA的解决方案。如果这对您有用,请告诉我,如果您找到了使用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

答案 2 :(得分:0)

在各种论坛上似乎有很多帖子表明 CDO 库不能与端口 587 一起使用,但事实并非如此。我认为真正的问题是使用端口 587 的 SMTP 服务这样做是因为它们需要 STARTTLS 身份验证,而 http://schemas.microsoft.com/cdo/configuration/smtpusessl 配置选项特定于 SSL,而不是 TLS (我知道,我知道 -不是最准确的描述,但现在就足够了)

相反,还有另一个设置 - 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()

答案 3 :(得分:-1)

我不知道你是否可以解决它,但我明白了:

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

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

请记得下载图书馆:

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

只需更换参数!