modify the scipt to use SMTP relay

Hi ,

We are using script in Active roles to send emails to the users. currently the script uses a username and a password to connect to exchange office365 as per below, we would like to use SMTP relay to modify the below script , can you please let me know how to change this script to use SMTP relay or any other way which is more secure rather than using txt file to call the password.

$smtpServer = "smtp.office365.com"
$from = "XXXX@abc.com"
$smtpServer = "smtp.office365.com"
$from = "XXXXX@abc.com"
$SmtpUser = "xxxxxxxxxx@abc.com"
$smtpPassword = Get-Content "D:\AR User\smtpcredabc.txt"
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)


Send-MailMessage -smtpServer $smtpServer -from $from -to $email -subject $subject -body $body -bodyasHTML -Port 587 -priority High -ErrorAction Stop -UseSsl -credential $cred -attachment $attachment

Parents
  • Hi  

    The below code from SMTP Test from PowerShell (MSSQLTips.com) gives a good guide on how to write PowerShell to Send an email, which would in part just be a matter of changing the SMTP server to your SMTP relay, plus the associated port.

    $SmtpServer = 'mysmtp.com' # SMTP Server name
    $Port = 25                 # SMTP server port number – default is 25
    $From = 'user@domain.com'  # from address - doesn't have to be valid, just in the format user@domain.ext and something your mail filter will not block
    $To = 'user@domain.ext'    # email address to send test to 
    $Subject = 'test'          # email subject
    $Body = 'test'             # email body
     
    Send-MailMessage -SmtpServer $SmtpServer -Port $Port -From $From -To $To -Subject $Subject -Body $Body 

    Also PowerShell: Script to Send Emails (KimConnect.com) has a good function, which you might be able to use.

Reply
  • Hi  

    The below code from SMTP Test from PowerShell (MSSQLTips.com) gives a good guide on how to write PowerShell to Send an email, which would in part just be a matter of changing the SMTP server to your SMTP relay, plus the associated port.

    $SmtpServer = 'mysmtp.com' # SMTP Server name
    $Port = 25                 # SMTP server port number – default is 25
    $From = 'user@domain.com'  # from address - doesn't have to be valid, just in the format user@domain.ext and something your mail filter will not block
    $To = 'user@domain.ext'    # email address to send test to 
    $Subject = 'test'          # email subject
    $Body = 'test'             # email body
     
    Send-MailMessage -SmtpServer $SmtpServer -Port $Port -From $From -To $To -Subject $Subject -Body $Body 

    Also PowerShell: Script to Send Emails (KimConnect.com) has a good function, which you might be able to use.

Children
No Data