I'm trying to set up email on SQL Server Reporting Services with Office 365. So I have properly configured Microsft tenant and mailbox.
For test on SQL machine I send an email using the Powershell Send-MailMessage script (details below):
$username = 'office#source.com'
$pass = get-content C:\pwd.txt | ConvertTo-SecureString #i'm have encoded password
$credentials=new-object Management.Automation.PSCredential -ArgumentList $username, $pass
$param = #{
SmtpServer = 'smtp.office365.com'
Port = 587
UseSsl = $true
Credential = $credentials
Encoding = 'utf8'
From = 'office#source.com'To = 'target#email.com'
Subject = '[TEST] subject for test'
Body = ' Testing SMTP from o365 '
}
Send-MailMessage #param
It's works OK, so the O365 and host configuration (e.g. network traffic, ports, tls) are correct.
Unfortunately, I am unable to run mails from SQL Server Reporting Services.
I'm try with difren setings of rsreportserver.config file. Part of setings below
<Configuration>
<RSEmailDPConfiguration>
<SMTPServer>smtp.office365.com</SMTPServer>
<SMTPServerPort>587</SMTPServerPort>
<SMTPAccountName>office#source.com</SMTPAccountName>
<SMTPConnectionTimeout></SMTPConnectionTimeout>
<SMTPServerPickupDirectory></SMTPServerPickupDirectory>
<SMTPUseSSL>True</SMTPUseSSL>
<SendUsing>2</SendUsing>
<SMTPAuthenticate>1</SMTPAuthenticate>
<SendUserName>AQAAANCMnd8BFdERjHo</SendUserName>
<SendPassword>RjHoAwE/Cl+sBAAAA0</SendPassword>
<From>office#source.com</From>
<EmbeddedRenderFormats>
<RenderingExtension>MHTML</RenderingExtension>
</EmbeddedRenderFormats>
<PrivilegedUserRenderFormats></PrivilegedUserRenderFormats>
<ExcludedRenderFormats>
<RenderingExtension>HTMLOWC</RenderingExtension>
<RenderingExtension>NULL</RenderingExtension>
<RenderingExtension>RGDI</RenderingExtension>
</ExcludedRenderFormats>
<SendEmailToUserAlias>False</SendEmailToUserAlias>
<DefaultHostName></DefaultHostName>
<PermittedHosts></PermittedHosts>
</RSEmailDPConfiguration>
</Configuration>
I'm reciving this error from Reporting Services:
Failure sending mail: At least one error has occurred.Mail will not be resent.
I found solution, You must add some registry values to your system:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727]
"SystemDefaultTlsVersions" = dword:00000001
"SchUseStrongCrypto" = dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SystemDefaultTlsVersions" = dword:00000001
"SchUseStrongCrypto" = dword:00000001
after restart OS, SSRS sends reports with Office 365 mailbox.
The issue is that the SMTP library used by SSRS is old and hasn't been updated. Forcing TLS 1.2 and Forcing it for .NET resolve the issue.
Related
I am using a FileZilla Server as an FTP. I want to create a PowerShell script that will send an email as soon as a file is uploaded.
I have been using the knowledge in this article: https://richjenks.com/filezilla-email-notifications/
and below is the code of my fn.ps1 file
$EmailFrom = "SENDER_EMAIL"
$EmailTo = "RECIPIENT_EMAIL"
$Subject = "New File Uploaded to FileZilla"
$Body = "A new file has been uploaded to the FTP server on FileZilla"
$SMTPServer = "127.0.0.1"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 21)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object
System.Net.NetworkCredential("GMAIL_ADDRESS", "GMAIL_PASSWORD");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
The only problem is that no email is actually sent? I can see the PowerShell pop up briefly after a file is uploaded so it is being run upon file upload, however no email is actually sent anywhere.
Any help would be greatly appreciated.
EDIT: I am not entirely sure what is the relationship between "SENDER_EMAIL" and "GMAIL_ADDRESS", I found the above solution on another website and they are different, should these not be the same?
Make sure you understand that the script actually has nothing to do with your FTP server. It is just a plain PowerShell script that sends a fixed email. So everything in the script is related to your SMTP mail server, not to your FTP server. So...
What goes to SmtpClient constructor is the address of your SMTP mail server. You seem to be using an address of your FTP server.
$SMTPServer = "127.0.0.1"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 21)
Check the example you have based this on. They use smtp.gmail.com:587.
Similarly, what goes to SmtpClient.Credentials are credentials to your SMTP mail server. So some real values, not placeholders like these:
$SMTPClient.Credentials = New-Object
System.Net.NetworkCredential("GMAIL_ADDRESS", "GMAIL_PASSWORD");
For Gmail they would be your Gmail address and password.
I've been trying to write a powershell script that sends an automated email as part of it's process using the logged in user's credentials. There are two primary methods that I've found to do this: Send-MailMessage and using a Net.Mail.SmtpClient object.
I've been able to get it up and running using the Net.Mail.SmtpClient object without issue, however that API is marked obsolete and so I'd prefer to not use it if possible, however I haven't been able to get Send-MailMessage to work using default credentials.
Without UseDefaultCredentials both of the scripts below fail with the same general message.
$smtpServer = "server.com"
$smtpFrom = "user#server.com"
$smtpTo = "user#server.com"
$messageSubject = "THIS IS WHERE YOUR SUBJECT GOES"
$messageBody = "This is a test of automated email at $([datetime]::Now.ToString('dd.MM.yyyy - HH.MM.SS'))"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.EnableSsl = $true
$smtp.Port = 587
#$smtp.UseDefaultCredentials = $true
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
and
$mailsplat = #{
SmtpServer = 'server.com'
From = 'user#server.com'
To = 'user#server.com'
Subject = 'THIS IS WHERE YOUR SUBJECT GOES'
Body = "This is a test of automated email at $([datetime]::Now.ToString('dd.MM.yyyy - HH.MM.SS'))"
Port = 587
UseSSL = $true
#UseDefaultCredentials = $true
}
Send-MailMessage #mailsplat
"The SMTP server requires a secure connection or the client was not authenticated. The server
response was: 5.7.1 Client was not authenticated"
Fixing the first script is easy. If I uncomment out the UseDefaultCredentials line it works perfectly as expected. There's no UseDefaultCredentials or similar parameter available to Send-MailMessage however. I'd prefer to use the Send-MailMessage as it's the recommended method and the other is marked as obsolete, but I can't actually find a way to make it work with my requirements (despite a plethora of posts saying Send-MailMessage will use default credentials if none are provided and the documentation indicating it should)
I have created a small PS script to create an email for my pipeline to send out whenever there is a deployment. the problem is i dont want the email to be sent from my personal email but from the company outlook email. i searched and saw different SMTP server names and using mail.from but i cant get it to work. can someone help me out?
param(
[Parameter(Mandatory=$true,Position=0)]
[string]$Address1,
[Parameter(Mandatory=$true,Position=1)]
[string]$Address2,
[switch]$Recurse,
[switch]$Force
)
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add($Address1)
$Mail.Recipients.Add($Address2)
$Mail.Subject = "DSC Deployment in Progress"
$Mail.Body = "There is a DSC install beginning. . ."
$Mail.Send()
You need to assign a value to the SendUsingAccount property. The account can be found in the (outlook).Session.Accounts collection.
$sendSmtpAddress = "some.name#somedomain.com"
$account = $ol.session.acounts | ? { $_.smtpAddress -eq $sendSmtpAddress }
then, assign to the SendUsingAccount property before sending
$mail.SendUsingAccount = $account
$mail.Send()
Full example
$sendSmtpAddress = "some.name#somedomain.com"
$ol = new-object -comobject "outlook.application"
$account = $ol.session.accounts | ? { $_.smtpAddress -eq $sendSmtpAddress }
$mail = $ol.CreateItem(0)
$mail.recipients.add("target.user#somedomain.com") | out-null
$mail.subject = "test email"
$mail.body = "test email"
$mail.SendUsingAccount = $account
$mail.Send()
For what it's worth, I gave up trying to send email via Outlook a long time ago, it's much easier to use plain SMTP. Depending on the security policy on your local SMTP server (Exchange?), you may be able to 'send as' any user on your local domain. Ask your IT people for the name/IP of an internal SMTP server that you can use to send email, and then it's as easy as:
send-mailmessage -smtpServer (servername or IP) -from sender.name#domain.com -to #(recipient1#domain.com, recipient2#domain.com) -subject "Email Subject" -body "Email Body"
If using send-mailmessage, it's possible to set a Display Name for the sender by using the form "Display Name <sender.name#domain.com>" e.g.
-from "Deployment Alerts <sender.name#domain.com>"
Recipients will see the Display Name in their email client, rather than the SMTP address.
A couple of points that I consider to be good practice:
Depending on the config of the SMTP server, there may be little, or no verification of the 'sender' address. It is worth using a genuine account that you have access to, so that you have sight of any bounce / non-delivery reports.
Consider including something in the mail body (perhaps a footer) that mentions where the alert came from and what process generated it. This can help your successor or colleague track down the script in the future.
The assignment like
$mail.SendUsingAccount = $account
worked for me since Windows Server 2000 till Windows Server 2008 and from Outlook 2007 till Outlook 2016. Since Windows Server 2016 I've got an exception (The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))).
But there is an alternative way to assign this property.
[Void] $mail.GetType().InvokeMember("SendUsingAccount","SetProperty",$NULL,$mail,$account)
I've always used the cmdlet Send-MailMessage without specifying any -Credential. Now I need to send mail using the anonymous user. The workaround I've found is this piece of code
$pass = ConvertTo-SecureString "anyString"-AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "NT AUTHORITY\ANONYMOUS LOGON",$pass
Send-MailMessage -Credential $cred #...
It works, but is this the correct method to get the anonymous is and send anonymous mail?
You can send mail from any address, it just depends on whether the receiving mail server cares about whether it can verify the sending user or not.
For example if you send from anon#microsoft.com and microsoft does not have that account, OR they have SPF records to indicate whether the sending mail server is valid or not, then the receiving mail server might(not always) reject it.
Just make sure your email user actually exists and has a valid domain... and you can send from anonymous# or noreply# or whatever you want to.
Okay, from the comments we know we're dealing with an Exchange server. Whether or not it will do an anonymous relay depends on the configuration of the Recieve Connectors. But those restrictions only apply to the network connections. If you run Send-MailMessage on the Exchange server and use 'LocalHost' as your SMTPServer it didn't go through a receive connector so those restrictions don't apply.
If you have remoting enabled on the Exchange server you can use that to do a local invocation to send email without having to modify the Receive Connector configurations:
$EmailParams =
#{
To = '<Email To>'
From = '<Email From>'
Subject = '<Email Subject>'
Body = '<Email Body>'
SMTPServer = 'localhost'
}
$Scriptblock = [Scriptblock]::Create(
"Send-MailMessage $(&{$args} #EmailParams) ")
Invoke-Command -ScriptBlock $Scriptblock -ComputerName ExchangeServer
I am facing unusual issue with Powershell script. I have written script to run DB queries which runs one STORED Procedure. Below is the complete script:
$server="DBServer,Inst"
$Database = "DATABASE"
$ConnectionTimeout = 30
$QueryTimeout = 120
$Query="STORED_PROCEDURE"
Import-Module “sqlps” -DisableNameChecking
$ds=Invoke-Sqlcmd -Query $Query -Database $Database -ServerInstance $server -ConnectionTimeout $ConnectionTimeout -QueryTimeout $QueryTimeout
$date=(Get-Date).ToString()
$ret=$ds.Return
--Query returns 0 or 1, 0 means SP Passed otherwise failed.
$From="abc#xyz.com"
$SendCC= "aabbcc.b#xyz.com"
If ($ret -eq '0')
{
$Body=" STORED_PROCEDURE: <FONT COLOR=#00FF00> <b>PASS </b> </FONT> in <b> $server</b>:<i>$Database</i>; TimeStamp: $date"
$msg="Test: STORED_PROCEDURE - SUCCESS "
Write-Host "Sending Mail Notification - Ret 0"|
Send-MailMessage -From $From -To $SendCC -Subject $msg ($Body |Out-String) -BodyAsHtml -SmtpServer mail.xyz.com
Write-Host "Notification Sent!!"
}
This script works absolutely fine from my computer which is in xyz domain. I have created batch file to run this script as:
#pushd "D:\Temp\SM_SP"
powershell -ExecutionPolicy Bypass -File .\SP_SM.ps1 -noconsole
#popd
Even this batch file works fine in my computer, sends mail.. Works as expected! But the real issue is running the same in one of the test server (win Ser 2008 R2). Where the batch file when executed manually returns no errors – however I am not getting any mail notification.
Note-
• I have mentioned proper smtp server details in script, as I said the same script and batch are working fine in my computer which is in the same domain as this server.
• I have tested Powershell inbuilt command “Send-MailMessage” in the server as:
Send-MailMessage -From $from -To $to -Subject Test -SmtpServer mail.xyz.com
Where $from and $to both are my address and am receiving the mail absolutely fine. But in the server when I execute the above script in Powershell console or through wrapped Batch file – am not receiving mail.
Any help on this is greatly appreciated. Thanks in advance!
Regards
-Raaj
I've run into this issue several times as a result of Receive Connector settings in Exchange. We typically will just add the IP of a server to allow it to relay if we are going to start using it for sending email, otherwise we want connections refused.
If you're using exchange go to Server Configuration->Hub Transport and check what you have listed under "Receive mail from remote servers that have these IP addresses" on the Network tab. If you're using some other mail server I'd refer to the documentation on connection permissions for it. Also, if you're using a different user account on the server than you are using on your desktop you may want to try a Run as different user and see if it's something with the permissions of the account you're using on the server.