【问题标题】:How to send emails by using VB.NET or C# code from an office 365 account that multi factor authentication enabled on it?如何使用启用了多因素身份验证的 Office 365 帐户中的 VB.NET 或 C# 代码发送电子邮件?
【发布时间】:2020-03-21 15:07:21
【问题描述】:

我有以下 VB.NET 代码用于从我的 Office 365 帐户发送电子邮件,在 Microsoft 强制在 Office 365 帐户上启用 MFA 之前,它一直运行良好:

Imports System.Net.Mail

Sub main()

        Dim message As MailMessage = New MailMessage()
        message.From = New MailAddress("MyEmail@MyOffice365Account.com")
        message.To.Add(New MailAddress("Receiver@AnyEmail.com"))       
        message.Subject = "My subject"
        message.Body = "My body message"
        message.IsBodyHtml = True
        message.BodyEncoding = System.Text.Encoding.UTF8
        message.SubjectEncoding = System.Text.Encoding.UTF8
        Dim smtp As New SmtpClient("smtp.office365.com", 587)
        smtp.EnableSsl = True
        smtp.Credentials = New Net.NetworkCredential("MyEmail@MyOffice365Email.com", "my password")
        smtp.Send(message)

End sub

当我运行上述代码时,我收到以下错误:

“System.Net.Mail.SmtpException”类型的未处理异常 发生在 System.dll 中

附加信息:SMTP 服务器需要安全连接 或客户端未通过身份验证。服务器响应为:5.7.57 SMTP;在 MAIL 期间,客户端未通过身份验证发送匿名邮件 FROM [DB6PR0501CA0038.eurprd05.prod.outlook.com]

我确定错误是在我为我的帐户启用多重身份验证后发生的,我无法禁用此功能,因为 Microsoft 已强制 Office 365 用户使用此功能。

有什么办法可以解决这个问题吗?

【问题讨论】:

标签: c# vb.net office365 smtpclient multi-factor-authentication


【解决方案1】:

您可以手动禁用 365 帐户的 MFA 状态。为此,您需要设置 365 Account Powershell。

使用 powershell,禁用 Office 365 域中 MSOL 用户的 MFA。

在您的 powershell 中,使用此代码检查所有具有 MFA 状态的 Msol 用户:

Get-MsolUser -all | select DisplayName,UserPrincipalName,@{N="MFA Status"; E={ if( $.StrongAuthenticationMethods.IsDefault -eq true) {(.StrongAuthenticationMethods | Where IsDefault -eq $True).MethodType} else { "Disabled"}}} | FT -AutoSize

然后要将 MFA 状态设置为已禁用,请使用以下代码:

Set-MsolUser -UserPrincipalName YourUserPrincipalNameHere -StrongAuthenticationRequirements @()

【讨论】:

    猜你喜欢
    • 2019-08-29
    • 2020-10-20
    • 2020-12-27
    • 2022-10-14
    • 2016-03-06
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多