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 {
        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
        "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.
        Send-SPTestMail -Web http://path.to/Web -From me@acme.com
        Sends a simple mail using all the nice deafults..
   param (
      [string]$From = "someone@example.com",
      [string]$Subject = $null,
      [string]$Body = $null

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


        $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
    } finally {
        if($spweb -ne $null) {