Dienstag, 17. Mai 2016

SharePoint eine Test-Mail versenden lassen

Auf die spannende Frage "Warum versendet SharePoint keine Mails?" gibt es in der Regel nur wenige Antworten.
Wenn die Einstellungen der ausgehenden Mails ("Configure outgoing e-mail settings") in SharePoint gut aussehen hilft meistens nur testen und das ULS-Log beobachten.

Das folgende PowerShell-Skript kann verwendet werden um schnell Test-Mails aus dem SharePoint versenden zu lassen (Dabei kann man dann das ULS-Log - am besten mittels ULS Viewer - beobachten)
function Send-SPTestMail {
  <#
      .SYNOPSIS
        Sends a Test-Mail using SharePoint-Standard Tools
        returns "TRUE", if the mail was successfully "taken in" with SharePoint (most probably also given to smtp...)
        and "FALSE" if there was any failure to do so.
        Check your ULS-Logs for further investigation if the result is FALSE, or your smtp-server-logs if the result is TRUE
  
      .PARAMETER To
        "TO" field of the mail.

      .PARAMETER Web
        The Url to the SPWeb to access via the SharePoint e-Mail utilities

      .PARAMETER From
        "FROM" field of the mail. defaults to "someone@example.com"
      
      .PARAMETER Subject
        "SUBJECT" field of the mail. Defaults to "Test" and a Date.

      .PARAMETER Body
        "BODY" of the mail. Defaults to some nice text.
  
      .EXAMPLE
        Send-SPTestMail -Web http://path.to/Web -From me@acme.com
        Sends a simple mail using all the nice deafults..
   #>
   [CmdletBinding()]
   param (
      [Parameter(Mandatory=$true)]
      [string]$Web,
      [Parameter(Mandatory=$true)]
      [string]$To,
      [Parameter(Mandatory=$false)]
      [string]$From = "someone@example.com",
      [Parameter(Mandatory=$false)]
      [string]$Subject = $null,
      [Parameter(Mandatory=$false)]
      [string]$Body = $null
   )

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
    $headers = new-object System.collections.Specialized.StringDictionary
    if(!$Subject) {
        $Subject = "Test @ $(Get-Date)";
    }

    $headers.add("to",$To)
    $headers.add("from",$From)
    $headers.add("Subject",$Subject)

    if(!$Body){
        $Body = "Auto-Generated body of test-mail <ul><li>Generated at $(Get-Date)</li><li>Headers:<ul>";
        $headers | % { $Body += "<li>$($_.Name): $($_.Value)</li>";  }
        $Body += "</ul></li><li>Sent via SharePoint-Web at $($Web)</li></ul>";
    }

    $spweb = $null;
    try {
        $spweb = Get-SPWeb $Web -ErrorAction Stop
        [Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($spweb,$headers,$Body)
    } finally {
        if($spweb -ne $null) {
            $spweb.Dispose();
        }
    }
 }

Keine Kommentare:

Kommentar veröffentlichen