通过 Outlook365 从 VB6/VB.net 4.0 应用程序发送电子邮件

Sending email via Outlook365 from VB6/VB.net 4.0 application

提问人:Slugsie 提问时间:10/10/2023 最后编辑:Slugsie 更新时间:10/10/2023 访问量:99

问:

我们目前有一个遗留的 VB6/VB.Net 4.0 应用程序(不幸的是,由于需要支持 XP,我们坚持使用这些应用程序),它的功能之一是发送电子邮件。它目前可以使用Ostrosoft SMTP组件与我们的各种客户端SMTP服务器一起使用。但是,它不适用于 Office365,因为它现在需要使用带有“StartTLS”的 TLS1.2,而 Ostrosoft 组件不支持。

我在 .Net 中尝试了各种方法,但似乎没有任何效果。

例如:

        Try
            Using SMTPMailServer As New Net.Mail.SmtpClient()
                Using MailMessage As Net.Mail.MailMessage = New Net.Mail.MailMessage()
                    SMTPMailServer.Port = 587
                    SMTPMailServer.Host = "smtp.office365.com"
                    SMTPMailServer.EnableSsl = True
                    SMTPMailServer.DeliveryMethod = SmtpDeliveryMethod.Network
                    SMTPMailServer.UseDefaultCredentials = False
                    SMTPMailServer.Credentials = New Net.NetworkCredential("**id**", "**password**")

                    MailMessage.From = New MailAddress(FromAddress)
                    MailMessage.To.Add(ToAddress)

                    MailMessage.Subject = $"Test Email {DateTime.Now}"
                    MailMessage.Body = "This is for testing SMTP mail using Method 1"

                    SMTPMailServer.Send(MailMessage)
                End Using

                MsgBox("Mail sent")
            End Using
        Catch ex As Exception
            MsgBox($"Error : {ex.Message}")
        End Try

这会导致以下错误:

SMTP 服务器需要安全连接,否则客户端未通过身份验证。服务器响应为:5.7.57 客户端未通过身份验证来发送邮件。错误:535 5.7.139 身份验证失败,请求不符合要成功进行身份验证的条件。

有人对此有解决方案吗?谢谢。

请注意,我已尝试将ServicePointManager.SecurityProtocol设置为3072,没有更改。

WinForms VB6 Office365 . NET-4.0 TLS1.2

评论

0赞 GSerg 10/10/2023
这回答了你的问题吗?.NET Framework 4.0 中的 TLS 1.2TLS 1.2 in .NET Framework 4.0
0赞 Slugsie 10/10/2023
@GSerg我看过那篇帖子,不幸的是它没有。那篇帖子似乎在谈论一个 WebForms 应用程序,其中它在 WinForms 中。但是,我尝试设置提到的注册表项和 ServicePointManager,没有区别。
0赞 GSerg 10/10/2023
它是winforms没有区别,你需要一个可以放在任何地方的代码行。虽然我意识到它对你不起作用,因为你实际上没有安装它工作所需的 4.5。
0赞 Hel O'Ween 10/10/2023
如果您可能愿意投资一个新库,请查看 SocketTools
0赞 Loathing 10/11/2023
解决方法是将邮件信息保存到网络驱动器,并在较新的操作系统上运行一些进程,以扫描文件夹并处理邮件的发送。或者另一个可能可行的想法是设置一个内部 SMTP(例如)将电子邮件中继给 smtp.office365.com,但我以前没有这样做过。SMTPMailServer.Host = "192.168.x.y";

答: 暂无答案