Tuesday, February 11, 2014

Send email with powershell with(out) SharePoint

If you are schedule a Job, an alert or just want to mess around with your colleagues. You can use this powershell script

a)

Add-PSSnapin Microsoft.SharePoint.Powershell
Write-Host "Sending Email...pausing 30 seconds"
Start-sleep -s 30
Write-Host "Waited 30 seconds"

#SMTP Server Name
$smtpServer = "exchange.com"

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

#Write FROM EMAIL
$from = "@something.com"
$msg.From = $from
$msg.ReplyTo = $from

#Write TO EMAIL
$msg.To.Add("@something.com")

#Your Subject
$msg.subject = "test subject"

#Your Email Body 
$msg.body = "test body"

#Sending email
$smtp.Send($msg)

1 - Write your smtp server
2 - Write email you want to be from
3 - Write email you want to send to

Or with SharePoint

b)
Add-PSSnapin Microsoft.SharePoint.Powershell

#Write EMAIL TO
$email = "@something.com"

#Write Subject
$subject = "Test subject"

#Write Body
$body = "Test body"

$web = Get-SPWeb "http://sharepointserver" #Email From have to be configured here
[Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,0,0,$email,$subject,$body)

1 - Write your smtp server
2 - Write email you want to be from
3 - Write email you want to send to


c ) Copy and save it a ps1 file and run it.


Error The application is unable to continue and will now terminated

Problem:
---------------------------
SharePoint Manager Preflight check Error
---------------------------
The application is unable to continue and will now terminated.
---------------------------
OK  
---------------------------

Solution:
You have to be Farm Administrator or SQL SysAdmin

Enjoy!