簡體   English   中英

如何使用 VB.NET 或 C# 代碼從啟用了多因素身份驗證的 Office 365 帳戶發送電子郵件?

[英]How to send emails by using VB.NET or C# code from an office 365 account that multi factor authentication enabled on it?

我有以下 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.dll 中發生類型為“System.Net.Mail.SmtpException”的未處理異常

附加信息:SMTP 服務器需要安全連接或客戶端未通過身份驗證。 服務器響應為:5.7.57 SMTP; 在 MAIL FROM [DB6PR0501CA0038.eurprd05.prod.outlook.com] 期間,客戶端未通過身份驗證發送匿名郵件

我確定錯誤是在我的帳戶上啟用多重身份驗證后發生的,我無法禁用此功能,因為 Microsoft 強制 Office 365 用戶使用此功能。

有沒有辦法解決這個問題?

您可以手動禁用 365 帳戶的 MFA 狀態。 為此,您需要設置 365 帳戶 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 @()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM