Sunday 14 December 2014

[FastToUse] Send mail with Exchange Web Service(EWS) using PowerShell

What?
How to send emails from inside or outside your organization with Exchange 2007/2010/2013/Online using PowerShell.

Fast instructions:
  • First of all you need to download and install the latest Microsoft EWS managed API.
  • After that,
    • Select the right DLL path
    • Replace the username, password, EWS url
    • copy&paste 
    • And run it in PowerShell!
PowerShell Code:

#Import the DLL of Microsoft EWS, choose one
##Version 2.2
Import-module "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
##For example if your version is 2.1 with x32 it will be
Import-module "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll"


#Create a Service Object and set Exchange 2010_SP2 version.
#If you don't set it, the default one is Exchange 2013
$version = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($version)

#Set credentials
$username = "domain\username"
$password = "Your_Password"
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $username, $password

#EWS URL, if you do not want to use autodiscover
$url = "https://mail.domain.com/EWS/Exchange.asmx"
$service.Url = $url

#Create the Object and send e-mail
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
$message.From = "mail@domain.com"
$message.ToRecipients.Add('Recipient Name','recipient_1@domain.com')
$message.ToRecipients.Add('Recipient Name','recipient_2@domain.com')
##You can add more recipients, copying the line before
$message.Subject = "This is a test email"
$message.Body = "Message body. Mail from EWS."

#Send email, choose one
##If you want to send it directly
$message.SendAndSaveCopy()
##If you do not want to send it, only to save to drafts and send later
$message.Save()

More information:



Hope it helps!

Monday 8 December 2014

[FastToUse] - Send mail with Exchange Web Service(EWS) using PHP

What?
How to send emails from inside or outside your organization with Exchange 2007/2010/2013/Online using PHP.

Fast instructions:
  • Go to GitHub PHP-EWS and download the libraries
  • Edit the following example and you will be sending e-mails with Exchange in a minutes.
PHP Code:
 <?php  
 //for debugging purpose only, remove in production  
 error_reporting(E_ALL);   
 ini_set( 'display_errors','1');  
 //Load libraries  
 require_once('ExchangeWebServices.php');  
 require_once('NTLMSoapClient.php');  
 require_once('NTLMSoapClient/Exchange.php');  
 require_once('EWS_Exception.php');  
 require_once('EWSType.php');  
 require_once('EWSType/MessageType.php');  
 require_once('EWSType/EmailAddressType.php');  
 require_once('EWSType/BodyType.php');  
 require_once('EWSType/SingleRecipientType.php');  
 require_once('EWSType/CreateItemType.php');  
 require_once('EWSType/NonEmptyArrayOfAllItemsType.php');  
 require_once('EWSType/ItemType.php');  
 class EwsSendEmail  
 {  
      protected function sendEmail()  
      {  
           $server = 'mail.server.com';  
           $username = 'domain\username';  
           $password = '';  
           $ews = new ExchangeWebServices($server, $username, $password);  
           $msg = new EWSType_MessageType();  
           $toAddresses = array();  
           $toAddresses[0] = new EWSType_EmailAddressType();  
           $toAddresses[0]->EmailAddress = 'user@domain.com';  
           $toAddresses[0]->Name = 'Name Surname';  
           $toAddresses[1] = new EWSType_EmailAddressType();  
           $toAddresses[1]->EmailAddress = 'user2@domain.com';  
           $toAddresses[1]->Name = 'Name2 Surname';  
           $msg->ToRecipients = $toAddresses;  
           $fromAddress = new EWSType_EmailAddressType();  
           $fromAddress->EmailAddress = 'mailFrom@domain.com';  
           $fromAddress->Name = 'NameFrom Surname';  
           $msg->From = new EWSType_SingleRecipientType();  
           $msg->From->Mailbox = $fromAddress;  
           $msg->Subject = 'Test missage from PHP code';  
           $msg->Body = new EWSType_BodyType();  
           $msg->Body->BodyType = 'HTML';  
           $msg->Body->_ = '<p style="font-size: 18px; font-weight: bold;">MESSAGE BODY!</p>';  
           $msgRequest = new EWSType_CreateItemType();  
           $msgRequest->Items = new EWSType_NonEmptyArrayOfAllItemsType();  
           $msgRequest->Items->Message = $msg;  
           $msgRequest->MessageDisposition = 'SendAndSaveCopy';  
           $msgRequest->MessageDispositionSpecified = true;  
           $response = $ews->CreateItem($msgRequest);  
           var_dump($response);  
      }  
      function __construct()  
      {  
           $this->sendEmail();  
      }  
 }  
 $page = new EwsSendEmail();  
 ?>  
More information:
Hope it helps!