How can I forward a message in Go msgraph-sdk-go and include Cc and/or Bcc recipients?

huangapple go评论86阅读模式
英文:

How can I forward a message in Go msgraph-sdk-go and include Cc and/or Bcc recipients?

问题

使用Go的msgraph-sdk-go库,我正在尝试转发一条消息。如果只有一个"To"收件人,我可以顺利完成,但如果有"Cc"和/或"Bcc"收件人,我无法将它们添加到消息中。

正常的requestBody(users.NewItemMailFoldersItemMessagesItemForwardPostRequestBody())与Forward().Post()函数一起使用,只有一个SetToRecipients()函数,但没有SetCcRecipients()SetBccRecipients()函数。

我尝试了各种方法来解决这个问题,但最接近的方法仍然不起作用。我不是调用Forward().Post(),而是调用CreateForward().Post(),它创建了一条消息的草稿,并返回消息对象(models.Messageable)。

然后,我可以在该消息对象上调用SetToRecipients()SetCcRecipients()和/或SetBccRecipients()函数。然后,我调用Patch()将更改保存到服务器,然后调用Send(),但是我一直收到一个错误,指示没有定义收件人。

我正在使用的代码(简化)如下:

func forwardMessage(id string, toRecipients []models.Recipientable, ccRecipients []models.Recipientable, bccRecipients []models.Recipientable) (success bool) {
	var err error
	var message models.Messageable
	var requestBody = users.NewItemMailFoldersItemMessagesItemForwardPostRequestBody()

	message, err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(id).CreateForward().Post(context.Background(), requestBody, nil)
	if err != nil {
		fmt.Println(getOdataErrorV1(err).Error())
		return false
	}

	message.SetToRecipients(toRecipients)
	if len(ccRecipients) > 0 {
		message.SetCcRecipients(ccRecipients)
	}
	if len(bccRecipients) > 0 {
		message.SetBccRecipients(bccRecipients)
	}

	message, err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(*message.GetId()).Patch(context.Background(), message, nil)
	if err != nil {
		fmt.Println(getOdataErrorV1(err).Error())
		return false
	}

	err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(*message.GetId()).Send().Post(context.Background(), nil)
	if err != nil {
		fmt.Println(getOdataErrorV1(err).Error())
		return false
	}

	return true
}

希望能帮助你解决问题!

英文:

Using the Go msgraph-sdk-go library, I am trying to forward a message. If I only have a "To" recipient, I can do it without issue but if I have "Cc" and/or "Bcc" recipients, I cannot get them added to the message.

The normal requestBody (users.NewItemMailFoldersItemMessagesItemForwardPostRequestBody()) used with the Forward().Post() function only has a SetToRecipients() function but does not have SetCcRecipients() or SetBccRecipients() functions.

I went through various attempts at working around this but the closest I have come still does not work. Instead of calling Forward().Post(), I call CreateForward().Post() instead, which creates a draft of the message, returning the message object (models.Messageable).

On that message object I am then able to call the SetToRecipients(), SetCcRecipients(), and/or SetBccRecipients() functions. I then call Patch() on the message to save the changes to the server, then I call Send() but I keep getting an error indicating there are no recipients defined.

The code I am working with is (simplified) as follows:

func forwardMessage(id string, toRecipients []models.Recipientable, ccRecipients []models.Recipientable, bccRecipients []models.Recipientable) (success bool) {
	var err error
	var message models.Messageable
	var requestBody = users.NewItemMailFoldersItemMessagesItemForwardPostRequestBody()

	message, err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(id).CreateForward().Post(context.Background(), requestBody, nil)
	if err != nil {
		fmt.Println(getOdataErrorV1(err).Error())
		return false
	}

	message.SetToRecipients(toRecipients)
	if len(ccRecipients) > 0 {
		message.SetCcRecipients(ccRecipients)
	}
	if len(bccRecipients) > 0 {
		message.SetBccRecipients(bccRecipients)
	}

	message, err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(*message.GetId()).Patch(context.Background(), message, nil)
	if err != nil {
		fmt.Println(getOdataErrorV1(err).Error())
		return false
	}

	err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(*message.GetId()).Send().Post(context.Background(), nil)
	if err != nil {
		fmt.Println(getOdataErrorV1(err).Error())
		return false
	}

	return true
}

Any help figuring this out would be appreciated!

答案1

得分: 0

经过来回尝试了许多不同的情况,我终于找到了解决方案!我必须:

  • 在调用createForward之前,将收件人应用于requestBody。
  • 在从该调用中获取新的(草稿)消息后,保存新的ID。
  • 初始化一个新的消息对象,并将抄送/密送收件人应用于该对象。
  • 调用Patch方法,将重新初始化的消息对象传递给newID消息。
  • 发送草稿消息。

更新后的代码如下:

func forwardMessage(id string, toRecipients []models.Recipientable, ccRecipients []models.Recipientable, bccRecipients []models.Recipientable) (success bool) {
    var err error
    var message models.Messageable
    var newID string
    var requestBody = users.NewItemMailFoldersItemMessagesItemForwardPostRequestBody()

    requestBody.SetToRecipients(toRecipients)

    message, err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(id).CreateForward().Post(context.Background(), requestBody, nil)
    if err != nil {
        fmt.Println(getOdataErrorV1(err).Error())
        return false
    }

    newID = *message.GetId()

    message = models.NewMessage()
    message.SetToRecipients(toRecipients)
    if len(ccRecipients) > 0 {
        message.SetCcRecipients(ccRecipients)
    }
    if len(bccRecipients) > 0 {
        message.SetBccRecipients(bccRecipients)
    }

    _, err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(newID).Patch(context.Background(), message, nil)
    if err != nil {
        fmt.Println(getOdataErrorV1(err).Error())
        return false
    }

    err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(newID).Send().Post(context.Background(), nil)
    if err != nil {
        fmt.Println(getOdataErrorV1(err).Error())
        return false
    }

    return true
}
英文:

After going back and forth and trying numerous different scenarios, I finally found the solution! I had to:

  • Apply the To recipients to the requestBody BEFORE I called createForward.
  • After I got the new (draft) message back from that call, I saved the new ID.
  • Initialize a NEW message object and apply the Cc/Bcc recipients to that.
  • Call Patch on the newID message, passing the re-initialized message object.
  • Send the draft message.

The updated code is as follows:

func forwardMessage(id string, toRecipients []models.Recipientable, ccRecipients []models.Recipientable, bccRecipients []models.Recipientable) (success bool) {
    var err error
    var message models.Messageable
	var newID string
    var requestBody = users.NewItemMailFoldersItemMessagesItemForwardPostRequestBody()

	requestBody.SetToRecipients(toRecipients)

    message, err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(id).CreateForward().Post(context.Background(), requestBody, nil)
    if err != nil {
        fmt.Println(getOdataErrorV1(err).Error())
        return false
    }

	newID = *message.GetId()
		
	message = models.NewMessage()
    message.SetToRecipients(toRecipients)
    if len(ccRecipients) > 0 {
        message.SetCcRecipients(ccRecipients)
    }
    if len(bccRecipients) > 0 {
        message.SetBccRecipients(bccRecipients)
    }

    _, err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(newID).Patch(context.Background(), message, nil)
    if err != nil {
        fmt.Println(getOdataErrorV1(err).Error())
        return false
    }

    err = pClient.Users().ByUserId(cpMailbox).Messages().ByMessageId(newID).Send().Post(context.Background(), nil)
    if err != nil {
        fmt.Println(getOdataErrorV1(err).Error())
        return false
    }

    return true
}

huangapple
  • 本文由 发表于 2023年5月20日 02:10:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76291550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定