How to make a script to send mail using MFA within a SSIS package

Geraldo Peralta 86 Reputation points
2024-08-24T17:46:35.1633333+00:00

Hello, everyone.

I have a Visual Basic script within a SSIS Script Task to send mail inside the organization. We are usinng Microsoft Azure to manage the users accounts. Now, Microsfot is enforcing the MFA.

Now we're getting this error:
User's image

This is the script that has been executing for years:

#Region "Help:  Introduction to the script task"
'The Script Task allows you to perform virtually any operation that can be accomplished in
'a .Net application within the context of an Integration Services control flow. 
'Expand the other regions which have "Help" prefixes for examples of specific ways to use
'Integration Services features within this script task.
#End Region

#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports System.Net.Mail
Imports Microsoft.SqlServer.Dts.Runtime
#End Region

Public Sub Main()
        '
        ' Add your code here
        '
        Dim varHTMLMail As MailMessage
        Dim varSMTPClient As SmtpClient
        Dim varDestinatarios As Object
        Dim contenido As String
        Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Ssl3 Or Net.SecurityProtocolType.Tls12 Or Net.SecurityProtocolType.Tls11 Or Net.SecurityProtocolType.Tls
        varDestinatarios = "user1@test.com"
        Try
            varHTMLMail = New MailMessage()
            varHTMLMail.From = New MailAddress("sender@test.com")
            varHTMLMail.Subject = "Insert Text here"
            contenido = "<head><style type=""text/css"" > h2, body {font-family: Arial, verdana;} table{font-size:11px; border-collapse:collapse;} td{background-color:#F1F1F1; border:1px solid black; padding:3px;} th{background-color:#99CCFF;}</style></head><body>"
            contenido = contenido + Dts.Variables("Insert Text0").Value.ToString()
            contenido = contenido + "<hr />"
            contenido = contenido + Dts.Variables("Insert Text1").Value.ToString()
            'contenido = contenido + "</body>"
            contenido = contenido + "<hr />"
            contenido = contenido + Dts.Variables("Insert Text2").Value.ToString()
            'contenido = contenido + "</body>"
            'MsgBox(contenido)
            varHTMLMail.Body = contenido
            varHTMLMail.To.Add("user1@test.com")
            varHTMLMail.To.Add("user2@test.com")
            varHTMLMail.To.Add("user3@test.com")
            varHTMLMail.To.Add("user4@test.com")
            varHTMLMail.To.Add("user5@test.com")
            varHTMLMail.IsBodyHtml = True
        Catch e As Exception
            MsgBox(e.ToString())
        End Try
        Try
            varSMTPClient = New SmtpClient("smtp.office365.com", 587)
            varSMTPClient.Credentials = New Net.NetworkCredential("sender@test.com", "Mypass")
            'varSMTPClient.UseDefaultCredentials = True
            varSMTPClient.EnableSsl = True
            varSMTPClient.Send(varHTMLMail)
        Catch e As Exception
            MsgBox(e.ToString)
        End Try
        Dts.TaskResult = ScriptResults.Success
    End Sub
#Region "ScriptResults declaration"
    'This enum provides a convenient shorthand within the scope of this class for setting the
    'result of the script.
    'This code was generated automatically.
    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum
#End Region
End Class
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,589 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,071 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dronec 171 Reputation points
    2024-08-25T11:00:06.5066667+00:00

    Looks like SMTP auth is disabled. See the following article to mitigate this:

    https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.