SharePoint – Send Files as Attachment

Hello everyone. Some days ago a colleague of mine asked me if it is possible to send files from SharePoint as an Attachment via mail. I told him it is possible and I want to show you how you can do this.

It is pretty simple to achieve this. All you need to do is the following:

  • Download the file from SharePoint and store it locally
  • Attach the file to your mail and send it
  • Delete the file

That´s all you need to do. But since we do not want to do it manually every time I created a short PowerShell Script which will do the work for me. (Of course you can do this in C# as well, but PowerShell seemed to be the quickest way to achieve this for me.)

Here is my Script:

#Get File from Webserver
function Get-FileFromWebServer
Param([parameter(Mandatory=$true)][alias("From")]$FromPath, [parameter(Mandatory=$true)][alias("To")]$ToPath)

#Split Path by "/" and store it in $Array
$Array = $FromPath.Split("/")

#Get filename from $Array
$FileName = $Array[($Array.Length-1)]

#Add filename to $ToPath
$ToPath = $ToPath + "\\" + $FileName

#Create new Instance of WebClient
$WebClient = New-Object System.Net.WebClient

#Use default credentials of the user account currently logged in
$WebClient.UseDefaultCredentials = $True
#Download the file from $FromPath and store it in $ToPath
$WebClient.DownloadFile($FromPath, $ToPath)

#Return $ToPath for later use
return $ToPath

#Send mail with Attachment
function Send-Mail
Param([parameter(Mandatory=$true)][alias("SMTP")]$SMTPServer, [parameter(Mandatory=$true)][alias("From")]$Sender, [parameter(Mandatory=$true)][alias("To")]$Receiver, [parameter(Mandatory=$true)][alias("Subject")]$SubjectString, [parameter(Mandatory=$true)][alias("Body")]$BodyMessage, [parameter(Mandatory=$true)][alias("Attachment")]$AttachmentFile)

#Create new SMTPClient Instance
$SMTPClient = New-Object Net.Mail.SMTPclient($SMTPServer)
#Create mail message object
$Message = New-Object Net.Mail.MailMessage($Sender, $Receiver, $SubjectString, $BodyMessage)
#Create Attachment and attach it to the mail message object
$Attachment = New-Object Net.Mail.Attachment($AttachmentFile)
#Send mail

#Dispose $Attachment and $Message

#Download file from SharePoint and send it via mail as Attachment
function Send-FileFromSharePointAsAttachment
Param([parameter(Mandatory=$true)][alias("FromPath")]$FromPathString, [parameter(Mandatory=$true)][alias("ToPath")]$ToPathString, [parameter(Mandatory=$true)][alias("SMTP")]$SMTPServer, [parameter(Mandatory=$true)][alias("From")]$Sender, [parameter(Mandatory=$true)][alias("To")]$Receiver, [parameter(Mandatory=$true)][alias("Subject")]$SubjectString, [parameter(Mandatory=$true)][alias("Body")]$BodyMessage)

#Download file from Webserver and store Download Path
$ToPathString = Get-FileFromWebServer -FromPath $FromPathString -ToPath $ToPathString
#Send the file as Attachment
Send-Mail -SMTP $SMTPServer -From $Sender -To $Receiver -Subject $SubjectString -Body $BodyMessage -Attachment $ToPathString
#Delete the file from Download Path
Remove-Item $ToPathString

#Set variables
$FromPath = ""
$ToPath = "%userprofile%\AppData\Local\Temp"
$SMTPServer = ""
$Sender = ""
$Receiver = ""
$Subject = "Attachment Test"
$Body = "This mail is a test."

#Execute Send-FileFromSharePointAsAttachment function
Send-FileFromSharePointAsAttachment -FromPath $FromPath -ToPath $ToPath -SMTP $SMTPServer -From $Sender -To $Receiver -Subject $Subject -Body $Body

That´s it. Now you know how you can send files from SharePoint as Attachment via mail. As always you can download my Script from here.


Tagged ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: