$PSEmailServer vs $smtpServer

Spunny 366 Reputation points
2022-03-11T15:40:18.5+00:00

Hi,
Requirement:
Powershell script run SSRS report and get response as pdf in Powershell script and email it as attachment. This powershell script gets called in sql job step. Not working. So, want to troubleshoot the script locally to make sure it is working.

**Try 1: ** NOT WORKING

   This is done as:   
          param(  
                  [string]$pRunDate  
              )  
   $pRunDate = Get-Date -Format "yyyy-MM-dd"  
   $url = "http://server/ReportServer?/TT/TR%20Pl%20UU%20GL%20TradeDate&pRunDate="  
   $uri = "$url$pRunDate&rs:Format=PDF"  
   $R = Invoke-WebRequest -Uri  $uri -UseDefaultCredentials   

2. Email $R stream
# Sender and Recipient Info
$MailFrom = "******@gmail.com"
$MailTo = "yy.gmail.com"

  # Server Info  
       $SmtpServer = "cxxx.xxxx"  
       #$SmtpPort = "25"   

  # Message stuff  
      $MessageSubject = "test"   
      $Message = New-Object System.Net.Mail.MailMessage $MailFrom,$MailTo  
      $Message.IsBodyHTML = $true  
      $Message.Subject = $MessageSubject  
      $Message.Body = 'Testing without content'  
  $stream = New-Object System.IO.MemoryStream -ArgumentList @(,$R.Content)  
    $attachment = New-Object Net.Mail.Attachment -ArgumentList @($stream, 'myreport.pdf')  

# Add the attachment  
$message.Attachments.Add($attachment)  

  # Construct the SMTP client object, credentials, and send  
  $Smtp = New-Object Net.Mail.SmtpClient($SmtpServer,$SmtpPort)  
  $Smtp.Send($Message)  

But this script run email doesn't work locally.

Try 2: DOESN'T WORK
So, I tested using send mail message
Send-MailMessage -From 'User01 <hh@Stuff .com>' -To 'User02 <yy@Stuff .com>' -Subject 'Test mail' -SmtpServer “<xxx.xx>”

Error: Send-MailMessage : The remote name could not be resolved: '<xxx.xx>'

Try 3: WORKS
$PSEmailServer = 'xxx.xx'

     Send-MailMessage -From '******@gmail.com' -To '******@gmail.com' -Subject 'Test mail'  

but I don't know how to attach memory stream object to Try 3 scenario

Any help or suggestion is appreciated.
I tried to google difference between $PSEmailServer Vs $SmtpServer

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-03-11T16:08:44.543+00:00

    Remove the "<" and ">" from the SmtpServer parameter value. Also, there's no need to protect the server name with quotation marks in the Send-MailMessage cmdlet.

    1 person found this answer helpful.

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.