3

I have the following code to simply add text to an email:

msg = New EmailMessage(Service)
With msg
    .From = New EmailAddress(Config("OutSender"))
    .ToRecipients.Add(New EmailAddress(Config("OutRecip")))
    .Subject = Config("OutSubject")

    .Load(New PropertySet(ItemSchema.Body))
    .Body.Text = Config("protMarking")
    .Send()
End With

I get the error:

You must load or assign this property before you can read its value

on this line:

.Body.Text = New MessageBody(Config("protMarking"))

Which is why I tried to load the body property on this line:

.Load(New PropertySet(ItemSchema.Body))

However I'm now getting this error:

This operation can't be performed because this service object doesn't have an Id

After extensive googling and a lot of looking at my code I cant figure it out. I'm not sure why the new email message doesn't have an ID?

NOTE: Service is call to a property that returns the ews service. I have check that this is working properly.

JabbaWook
  • 677
  • 1
  • 8
  • 25

2 Answers2

3

You should just set the Body property (not Body.Text)

The call to Load is not necessary in your case. For reference though, it isn't working as you haven't yet created the email message on the service. That only happens when you get to Send (which should probably be SendAndSaveCopy()

msg = New EmailMessage(Service)
With msg
    .From = New EmailAddress(Config("OutSender"))
    .ToRecipients.Add(New EmailAddress(Config("OutRecip")))
    .Subject = Config("OutSubject")
    .Body = Config("protMarking")
    .SendAndSaveCopy()
End With
PaulG
  • 13,871
  • 9
  • 56
  • 78
1

using @PaulG answer and some trial and error work prior to that, this was my solution.

msg = New EmailMessage(Service)
With msg
    .From = New EmailAddress(Config("OutSender"))
    .ToRecipients.Add(New EmailAddress(Config("OutRecip")))
    .Subject = Config("OutSubject")
    .Body = New MessageBody(Config("protMarking"))
    .SendAndSaveCopy()
End With

with this line being the major change/fix:

.Body = New MessageBody(Config("protMarking"))
JabbaWook
  • 677
  • 1
  • 8
  • 25