mailserver, powershell,

PowerShell send E-Mail

agowa338 agowa338 Mar 24, 2016 · 4 mins read
Share this

Without smtp Server Authentication (with static IP or internal Mail Server)

Note: Most public mail servers only accept a message for there users and don't forward it to other servers.

 $DNSHostName = (Get-WmiObject
  win32_computersystem).DNSHostName; $DNSDomainName = (Get-WmiObject
  win32_computersystem).Domain; $From =
  "PowerShellMonitoring@$DNSHostName+'.'+$DNSDomainName"; $To =
  "edv@$DNSHostName+'.'+$DNSDomainName"; $Subject = "Failure
  $(@($isOnline).Count) Hosts down"; $SMTPServer = @(Resolve-DnsName -Name
  $DNSDomainName -Type MX | Select-Object -First 1).NameExchange; $Port = 465
  $Body = "MESSAGE BODY"; Send-MailMessage -From $From -Subject $Subject -To $To
  -Body $Body -SmtpServer $SMTPServer -UseSsl -Port $Port; 

With authentication (dynamic ip or no internal Mail Server)

Note: Some public mail servers only accept the message if the "From-Address" is a valid DNS name (even

 $DNSHostName = (Get-WmiObject
  win32_computersystem).DNSHostName; $DNSDomainName = (Get-WmiObject
  win32_computersystem).Domain; $From =
  "PowerShellMonitoring@$DNSHostName+'.'+$DNSDomainName"; $To =
  "edv@$DNSHostName+'.'+$DNSDomainName"; $Subject = "Failure
  $(@($isOnline).Count) Hosts down"; $SMTPServer = @(Resolve-DnsName -Name
  $DNSDomainName -Type MX | Select-Object -First 1).NameExchange; $Port = 25
  $Credential = New-Object System.Management.Automation.PSCredential("USERNAME",
  $(ConvertTo-SecureString "PASSWORD" -AsPlainText -Force)) $Body = "MESSAGE
  BODY"; Send-MailMessage -From $From -Subject $Subject -To $To -Body $Body
  -SmtpServer $SMTPServer -UseSsl -Credential $Credential -Port $Port; 

With Outlook Api (so the message shows up in sent items)

Note: This approach should only be used if this application already requires outlook for other features. It is generally not recommended to use this because of stability and deadlock issues. Even though it should work in most cases, applications that use the com API of Outlook have proven to be verry fragile in production environments. Common issues are the script timeouting or crashing as well as Outlook deadlocking. Most of the time this is caused by the user having many (or complex) filter rules or by mailboxes with high itemcount.

 # Folders $FolderReports = "D:\Reports"; # Where
  your reports are # SQL-Server settings $Database = "Database" # Database name
  $Server = "SERVER\SQLEXPRESS"; # SQL-Server Instanz # Connect to SQL and query
  data, extract data to SQL Adapter $SqlQuery = "SELECT
  [Report],[Filiale],[E-Mail] FROM [dbo].[verteiler]"; # Your distributiontable
  # The mail $MailSubject = "Enter subjectline here" $MailBody = ' Dear
  Customer,<br />
  .......<br />
  ......<br />
  .....<br />
  ' # Can be plain text or html # To use this script you must have a database
  that looks like this: ## Database Layout ## "Report","Filiale","E-Mail" ##
  "012","12","" ## "033","33","" ##
  "112","112","" $SqlConnection = New-Object
  System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Data
  Source=$Server;Initial Catalog=$Database;Integrated Security = True" $SqlCmd =
  New-Object System.Data.SqlClient.SqlCommand $SqlCmd.CommandText = $SqlQuery
  $SqlCmd.Connection = $SqlConnection $SqlAdapter = New-Object
  System.Data.SqlClient.SqlDataAdapter $SqlAdapter.SelectCommand = $SqlCmd
  $DataSet = New-Object System.Data.DataSet $nRecs = $SqlAdapter.Fill($DataSet)
  $nRecs | Out-Null $objTable = $DataSet.Tables[0] # Send the E-Mail using
  Outlook $ol = New-Object -comObject Outlook.Application $objTable |
  ForEach-Object { $mail = $ol.CreateItem(0) $Mail.To = $_."E-Mail" $Mail.BCC =
  $mailBCC $Mail.Subject = $MailSubject if ($MailBody -like '*
  *') { $Mail.HTMLBody = $MailBody } else { $Mail.Body = $MailBody }
  $Mail.Attachments.Add("$FolderReports\$($_."Report").pdf") $Mail.Send() } 

Written by agowa338