Exception calling "Send" with "1" argument(s): The server response was: Cannot connect to SMTP server, connect error 10060"

john john 1,021 Reputation points
2022-12-15T10:55:23.31+00:00

I have the following code to connect to ExchangeOnline >> generate csv file >> send the file by email using Office 365 smtp:-

Connect-ExchangeOnline  
$SiteIDs = '64898c8f-2d5f-4e0e-9a9b-eb9828975a9e','20e6140c-0441-4988-b36c-c61cf3400847'  
$Operations = @('FileAccessed','FileDownloaded','FileDeleted')  
$OutputFile = ".\UnifiedAuditLog_FULL.csv"  
$Today = Get-Date -Date (Get-Date -Format “yyyy-MM-dd”)  
$intDays = 14  
For ($i=0; $i -le $intDays; $i++){  
  For ($j=23; $j -ge 0; $j--){  
    $StartDate = ($Today.AddDays(-$i)).AddHours($j)  
    $EndDate = ($Today.AddDays(-$i)).AddHours($j + 1)  
    $Audit = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -ResultSize 5000 -SiteIds $SiteIDs -RecordType SharePointFileOperation -Operations $Operations   
    $ConvertAudit = $Audit | Select-Object -ExpandProperty AuditData | ConvertFrom-Json  
 $OutputFile0 = ".\UnifiedAuditLog_FULL"+$i+$j+"ALL0.csv"  
    $ConvertAudit | Select-Object CreationTime,UserId,Operation,Workload,ObjectID,SiteUrl,SourceFileName,ClientIP,UserAgent | Export-Csv $OutputFile0 -NoTypeInformation -Force -Append  
    Write-Host $StartDate `t $Audit.Count  
 # File to be attached in email  
$attachment = new-object Net.Mail.Attachment("D:\"+$OutputFile0)  
  
# Configure SMTP server  
$smtpServer = "smtp.office365.com"  
$mailMessage = new-object Net.Mail.MailMessage  
$smtpObj = new-object Net.Mail.SmtpClient($smtpServer)  
  
# Set email parameters  
$mailMessage.From = "***"  
$mailMessage.ReplyTo = "****"  
$mailMessage.To.Add("****")  
$mailMessage.subject = "[MAIL SUBJECT]"  
$mailMessage.body = "[MAIL BODY]"  
$mailMessage.Attachments.Add($attachment)  
  
# Send email  
$smtpObj.Send($mailMessage)  
$attachment.Dispose()  
    
    
    
    
    
  }  
}  
Disconnect-ExchangeOnline  

But i will get this exception:-

Exception calling "Send" with "1" argument(s): "Service not available, closing transmission channel. The server  
response was: Cannot connect to SMTP server 40.101.76.178 (40.101.76.178:25), connect error 10060"  
At line:28 char:1  
+ $smtpObj.Send($mailMessage)  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException  
    + FullyQualifiedErrorId : SmtpException  

Any advice?

Update-1

Now when i define the port number as follow:-

$smtpObj = new-object Net.Mail.SmtpClient($smtpServer,587)  

I will get this error instead:-

Exception calling "Send" with "1" argument(s): "Error in processing. The server response was: 5.7.3 STARTTLS is  
required to send mail [VI1P194CA0025.EURP194.PROD.OUTLOOK.COM]"  
At line:28 char:1  
+ $smtpObj.Send($mailMessage)  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException  
    + FullyQualifiedErrorId : SmtpException  

Update-2 I also tried this:-

Read-Host -Prompt "Enter your tenant password" -AsSecureString | ConvertFrom-SecureString | Out-File "O365.key"  
  
Connect-ExchangeOnline  
$SiteIDs = '64898c8f-2d5f-4e0e-9a9b-eb9828975a9e','20e6140c-0441-4988-b36c-c61cf3400847'  
$Operations = @('FileAccessed','FileDownloaded','FileDeleted')  
$OutputFile = ".\UnifiedAuditLog_FULL.csv"  
$Today = Get-Date -Date (Get-Date -Format “yyyy-MM-dd”)  
$intDays = 2  
$TenantPass = cat "O365.key" | ConvertTo-SecureString  
$emailCredential = New-Object System.Net.NetworkCredential("test.user@***.com", $TenantPass)  
For ($i=0; $i -le $intDays; $i++){  
  For ($j=23; $j -ge 0; $j--){  
    $StartDate = ($Today.AddDays(-$i)).AddHours($j)  
    $EndDate = ($Today.AddDays(-$i)).AddHours($j + 1)  
    $Audit = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -ResultSize 5000 -SiteIds $SiteIDs -RecordType SharePointFileOperation -Operations $Operations   
    $ConvertAudit = $Audit | Select-Object -ExpandProperty AuditData | ConvertFrom-Json  
 $OutputFile0 = ".\UnifiedAuditLog_FULL"+$i+$j+"ALL0.csv"  
    $ConvertAudit | Select-Object CreationTime,UserId,Operation,Workload,ObjectID,SiteUrl,SourceFileName,ClientIP,UserAgent | Export-Csv $OutputFile0 -NoTypeInformation -Force -Append  
    Write-Host $StartDate `t $Audit.Count  
  
 # File to be attached in email  
$attachment = new-object Net.Mail.Attachment("D:\"+$OutputFile0)  
  
# Configure SMTP server  
$smtpServer = "smtp.office365.com"  
$mailMessage = new-object Net.Mail.MailMessage  
$smtpObj = new-object Net.Mail.SmtpClient($smtpServer,587)  
$smtpObj.EnableSSl = $true  
$smtpObj.Credentials = $emailCredential  
# Set email parameters  
$mailMessage.From = "test.user@***.com"  
$mailMessage.To.add("test.user@***.com")  
$mailMessage.subject = "[MAIL SUBJECT]"  
#$mailMessage.body = "[MAIL BODY]"  
#$mailMessage.Attachments.Add($attachment)  
  
# Send email  
$smtpObj.Send($mailMessage)  
$attachment.Dispose()  
    
    
    
    
    
  }  
}  
Disconnect-ExchangeOnline  

but i got this error:-

Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not  
authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139  
Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your  
administrator. [VI1P191CA0005.EURP191.PROD.OUTLOOK.COM]"  
At line:26 char:1  
+ $smtpObj.Send($mailMessage)  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException  
    + FullyQualifiedErrorId : SmtpException  

Although the user i am using has SmtpClientAuthenticationDisabled = False, as follow

PS D:\> Get-CASMailbox -Identity test.user@***.com  
Name          ActiveSyncEnabled OWAEnabled PopEnabled ImapEnabled MapiEnabled SmtpClientAuthenticationDisabled  
----          ----------------- ---------- ---------- ----------- ----------- --------------------------------  
test.user@***.com True              True       True       True        True        False  
Exchange | Exchange Server | Development
{count} votes

1 answer

Sort by: Most helpful
  1. Rafael da Rocha 5,251 Reputation points
    2022-12-15T11:00:04.477+00:00

    Hello,

    here's a thread that solved the same error for another user:
    535-57139-authentication-unsuccessful.html

    0 comments No comments

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.