none
Please Help! Send an email using REST API in Powershell RRS feed

  • Question

  • Hi

    A little new to this. After successfully retrieving email from my Office 365 mailbox (we have a hybrid deployment) using a powershell one liner (based on Invoke-RestMethod...) I wanted to send a test email (to anyone I don't care at this point!)

    This is what I have so far.. I know its wrong... can anyone help.. 

    As if its no surprise I'm getting the usual Invoke-RestMethod : The remote server returned an error: (400) Bad Request.


    $uri = "https://outlook.office365.com/api/v1.0/me/sendmail"


    $UserName = "andrew.stevens@my.domain"
    $Password = cat C:\MydomainCreds.txt | convertto-securestring
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password


    $body = "{
     ""Subject"": ""This is a send test"",
     ""Importance"": ""High"",
     ""Body"": {
     ""ContentType"": ""HTML"",
     ""Content"": ""How about this for a surprise!""
     },
     ""ToRecipients"": [
     {
      ""Address"": ""mytestmailbox@anywhere.com""
     }
     ]
     }"



    Invoke-RestMethod -Uri $uri -Method Post -Credential $cred -ContentType "application/json" -Body $Body

    Thursday, June 30, 2016 2:28 PM

Answers

  • What about this..

    $contentType = "application/json;odata.metadata=full"
     $uri = "https://outlook.office365.com/api/v1.0/me/sendmail"
     $body = "{
    ""Message"":{
     ""Subject"": ""This is a send test"",
     ""Importance"": ""High"",
     ""Body"": {
     ""ContentType"": ""HTML"",
     ""Content"": ""How about this for a surprise!""
     },
     ""ToRecipients"": [
     {
     ""EmailAddress"":{
      ""Address"": ""mytestmailbox@anywhere.com""
      }
     }
     ]
     }}"
     
     
     Invoke-RestMethod -Uri $uri -Method Post -Credential $cred -Body $Body -ContentType $contentType

    Monday, July 4, 2016 11:00 AM

All replies

  • You should use Send-MailMessage.

    Start with this: https://msdn.microsoft.com/office/office365/howto/add-common-consent-manually


    \_(ツ)_/

    Thursday, June 30, 2016 3:46 PM
  • I could but that isnt what I need..

    I'm trying to follow something like the following.. (but using api v1.0)

    This is found here:

    https://sites.utexas.edu/glenmark/2014/04/30/exchange-online-mailbox-access-from-powershell-via-rest/

    By the way why do I always get page not found at the dev site?

    PS C:\> $uri = "https://outlook.office365.com/ews/odata/Users('its.jimi.hendrix@austin.utexas.edu')/Drafts/Messages?'" PS C:\> $contentType = "application/json;odata.metadata=full" PS C:\> $body = "{ >> ""@odata.type"": ""#Microsoft.Exchange.Services.OData.Model.Message"", >> ""Subject"": ""This is a send test"", >> ""Importance"": ""High"", >> ""Body"": { >> ""ContentType"": ""HTML"", >> ""Content"": ""I'm sending this via REST!"" >> }, >> ""ToRecipients"": [ >> { >> ""Name"": ""Test Mailbox"", >> ""Address"": ""testmbox@austin.utexas.edu"" >> } >> ] >> }" >> PS C:\> $new= Invoke-RestMethod -Uri $uri -Method POST -ContentType $contentType -Credential $cred -Body $body

    Thursday, June 30, 2016 4:15 PM
  • I could but that isnt what I need..

    Why not?


    -- Bill Stewart [Bill_Stewart]

    Thursday, June 30, 2016 4:20 PM
    Moderator
  • Now I could be wrong.. but wouldnt send-mailmessage require a relay?

    //A

    Thursday, June 30, 2016 4:42 PM
  • Now I could be wrong.. but wouldnt send-mailmessage require a relay?

    //A

    Send-MailMessage -From me@myo365account.net -To jsmith@goobers.com -SmtpServer smtp.office365.com -port 587 -subject test -body 'from PowerShell' -cred $cred -UseSsl

    Works fine for me.


    \_(ツ)_/


    • Edited by jrv Thursday, June 30, 2016 7:33 PM
    Thursday, June 30, 2016 7:32 PM
  • I prefer this method:

    # at beginning of script
    $mailprops=@{
    	From='me@myo365account.net'
    	SmtpServer='smtp.office365.com'
    	Port=587
    	UseSsl=$true
    	Credential=$cred
    }
    
    # later in script
    Send-MailMessage -To jsmith@goobers.com -subject test -body 'from PowerShell' @mailprops


    \_(ツ)_/

    Thursday, June 30, 2016 7:37 PM
  • This is great and it works..but please, if only for my own satisfaction how can you do it using the rest api?

    I hope someone can help

    //A

    Thursday, June 30, 2016 8:43 PM
  • This is great and it works..but please, if only for my own satisfaction how can you do it using the rest api?

    I hope someone can help

    //A

    You have to have a registered app or the API will not work.  The docs you have are old and the system has been changed.


    \_(ツ)_/

    Thursday, June 30, 2016 8:56 PM
  • Look I'm not a developer. That's clear. I'd like to think I know a thing or two about Powershell. I'm just interested in knowing if I can send an email using the REST API. I can travel to Scotland using a car, train or walk; going by foot may not be the most convenient method but one I might fancy doing. You learn a lot more on the way. I don't want a philosophical argument I just want to know if its possible. I started reading about the API and and just wanted to extend my knowledge. If you dont know the answer then fine. But if you do know then please share it. 

    //A

    Thursday, June 30, 2016 10:09 PM
  • You have to start by registering your App with Microsoft to obtain an app token.  You then need to review the documentation on how the API is designed and how to use it.

    Also note that the REST API for Azure was not designed to be used from PowerShell.  There are no examples and no discussions about using the current API.  The older API does not work with the newer O365 platform as far as I can tell.

    THe APIs werer designed to support application developers and not for end user use.  THe apps are registered because they will beused by many people.

    Download Visual Studio and the API support for VS and it will help you register and develop an application in C#, PHP or other language. After you have understood the examples then you might find a way to use the API with PowerShell.

    IF you wait a couple of week someone will likely do this and publish it.


    \_(ツ)_/

    • Proposed as answer by marvhen Sunday, July 3, 2016 9:43 AM
    Thursday, June 30, 2016 10:16 PM
  • Thanks.. you have been very helpful..I will take your advice and hope for the best!

    //A

    Thursday, June 30, 2016 10:19 PM
  • What about this..

    $contentType = "application/json;odata.metadata=full"
     $uri = "https://outlook.office365.com/api/v1.0/me/sendmail"
     $body = "{
    ""Message"":{
     ""Subject"": ""This is a send test"",
     ""Importance"": ""High"",
     ""Body"": {
     ""ContentType"": ""HTML"",
     ""Content"": ""How about this for a surprise!""
     },
     ""ToRecipients"": [
     {
     ""EmailAddress"":{
      ""Address"": ""mytestmailbox@anywhere.com""
      }
     }
     ]
     }}"
     
     
     Invoke-RestMethod -Uri $uri -Method Post -Credential $cred -Body $Body -ContentType $contentType

    Monday, July 4, 2016 11:00 AM
  • What about this..

    What happens when you try it?


    -- Bill Stewart [Bill_Stewart]

    Monday, July 4, 2016 10:26 PM
    Moderator
  • It works! Now how would I move messages from one folder to another?
    Tuesday, July 5, 2016 7:31 AM
  • Different question. Please ask in a more appropriate forum. (This is not an Exchange or Office 365 forum.)

    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 5, 2016 2:10 PM
    Moderator