Saturday, May 12, 2012

SharePoint Incoming Email Configuration - PowerShell

PowerShell is a strong windows shell scripting, which is widely used in SharePoint 2010. Here I am sharing small and useful scripts we use for SharePoint Farm administration.

Requirement:
Configure Incoming Email settings in SharePoint farm.


Script:

# *******************************************************************
## Function - Configure Incoming Email settings in SharePoint farm
## Author - Deepak Solanki
## Checks to ensure that Microsoft.SharePoint.Powershell is loaded, if not, adding pssnapin
## Configure Incoming Email settings in SharePoint farm
# ********************************************************************
#Add SharePoint Add-ins
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue 
# if snapin is not installed then use this method
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


Write-Host "Script started to Configure Incoming Email." 


  #Variables to get config values    
 [boolean]$Enabled = $true;
 [boolean]$UseAutomaticSettings = $false;
 [boolean]$UseDirectoryManagementService = $true;
[boolean]$RemoteDirectoryManagementService = $false;
$ServerAddress = "EXCHANGE.DOMAIN.COM";
[boolean]$DLsRequireAuthenticatedSenders = $true;
[boolean]$DistributionGroupsEnabled = $true;
$ServerDisplayAddress = "sharepoint.company.com";
$DropFolder = "c:\inetpub\mailroot\drop";

#Test the drop folder location exists before proceeding with any changes
$dropLocationTest = Get-Item $DropFolder -ErrorAction SilentlyContinue
if ($dropLocationTest -eq $null)
{
Throw "The drop folder location $DropFolder does not exist - please create the path and try the script again."
}

    #Configuring Incoming E-mail Settings
    try
    {
$type = "Microsoft SharePoint Foundation Incoming E-Mail"
$svcinstance = Get-SPServiceInstance | where { $_.TypeName -eq $type}
$inmail = $svcinstance.Service


if ($inmail -ne $null)
        {
Write-Log "Configuring Incoming E-mail Settings."            
#Enable sites on this server to receive e-mail
                $inmail.Enabled = $Enabled
            
#Automatic Settings mode
                $inmail.UseAutomaticSettings = $UseAutomaticSettings
            
#Use the SharePoint Directory Management Service to create distribution groups
                $inmail.UseDirectoryManagementService = $UseDirectoryManagementService
            
#Use remote: Directory Management Service
                $inmail.RemoteDirectoryManagementService = $RemoteDirectoryManagementService
            
#SMTP mail server for incoming mail
                $inmail.ServerAddress = $ServerAddress            

#Accept messages from authenticated users only
                $inmail.DLsRequireAuthenticatedSenders = $DLsRequireAuthenticatedSenders
            
#Allow creation of distribution groups from SharePoint sites
                $inmail.DistributionGroupsEnabled = $DistributionGroupsEnabled
            
#E-mail server display address
                $inmail.ServerDisplayAddress = $ServerDisplayAddress
            
#E-mail drop folder
                $inmail.DropFolder = $DropFolder;  

$inmail.Update();
Write-Host "Incoming E-mail Settings completed."
        } 
    }
    #Report if there is a problem setting Incoming Email
    catch { Write-Host "There was a problem setting Incoming Email: $_" }

5 comments:

  1. how to get incoming email in sharepoint and can i use "sharepoint.company.com" as for display name.

    pls suggest me...

    ReplyDelete
  2. Nicely Done! Thank You

    ReplyDelete
  3. My hero ;)
    You are the only one who covered that topic with powershell.

    ReplyDelete
  4. properties are not identifying...any suggestions

    ReplyDelete