通过


使用 Microsoft Graph 向 Go 应用添加电子邮件功能

在本文中,你将使用 Microsoft Graph 邮件 API 扩展在 生成 Go 应用中 创建 Microsoft的应用程序。 使用 Microsoft Graph 列出用户的收件箱并发送电子邮件。

列出用户的收件箱

首先,在用户的电子邮件收件箱中列出邮件。

  1. 将以下函数添加到 ./graphhelper/graphhelper.go

    func (g *GraphHelper) GetInbox() (models.MessageCollectionResponseable, error) {
        var topValue int32 = 25
        query := users.ItemMailFoldersItemMessagesRequestBuilderGetQueryParameters{
            // Only request specific properties
            Select: []string{"from", "isRead", "receivedDateTime", "subject"},
            // Get at most 25 results
            Top: &topValue,
            // Sort by received time, newest first
            Orderby: []string{"receivedDateTime DESC"},
        }
    
        return g.userClient.Me().MailFolders().
            ByMailFolderId("inbox").
            Messages().
            Get(context.Background(),
                &users.ItemMailFoldersItemMessagesRequestBuilderGetRequestConfiguration{
                    QueryParameters: &query,
                })
    }
    
  2. graphtutorial.go 中的空listInbox函数替换为以下内容。

    func listInbox(graphHelper *graphhelper.GraphHelper) {
        messages, err := graphHelper.GetInbox()
        if err != nil {
            log.Panicf("Error getting user's inbox: %v", err)
        }
    
        // Load local time zone
        // Dates returned by Graph are in UTC, use this
        // to convert to local
        location, err := time.LoadLocation("Local")
        if err != nil {
            log.Panicf("Error getting local timezone: %v", err)
        }
    
        // Output each message's details
        for _, message := range messages.GetValue() {
            fmt.Printf("Message: %s\n", *message.GetSubject())
            fmt.Printf("  From: %s\n", *message.GetFrom().GetEmailAddress().GetName())
    
            status := "Unknown"
            if *message.GetIsRead() {
                status = "Read"
            } else {
                status = "Unread"
            }
            fmt.Printf("  Status: %s\n", status)
            fmt.Printf("  Received: %s\n", (*message.GetReceivedDateTime()).In(location))
        }
    
        // If GetOdataNextLink does not return nil,
        // there are more messages available on the server
        nextLink := messages.GetOdataNextLink()
    
        fmt.Println()
        fmt.Printf("More messages available? %t\n", nextLink != nil)
        fmt.Println()
    }
    
  3. 运行应用,登录,然后选择选项 2 列出收件箱。

    Please choose one of the following options:
    0. Exit
    1. Display access token
    2. List my inbox
    3. Send mail
    4. Make a Graph call
    2
    Message: Updates from Ask HR and other communities
      From: Contoso Demo on Yammer
      Status: Read
      Received: 2021-12-30 04:54:54 -0500 EST
    Message: Employee Initiative Thoughts
      From: Patti Fernandez
      Status: Read
      Received: 2021-12-28 17:01:10 -0500 EST
    Message: Voice Mail (11 seconds)
      From: Alex Wilber
      Status: Unread
      Received: 2021-12-28 17:00:46 -0500 EST
    Message: Our Spring Blog Update
      From: Alex Wilber
      Status: Unread
      Received: 2021-12-28 16:49:46 -0500 EST
    Message: Atlanta Flight Reservation
      From: Alex Wilber
      Status: Unread
      Received: 2021-12-28 16:35:42 -0500 EST
    Message: Atlanta Trip Itinerary - down time
      From: Alex Wilber
      Status: Unread
      Received: 2021-12-28 16:22:04 -0500 EST
    
    ...
    
    More messages available? True
    

GetInbox 说明

请考虑 函数中的 GetInbox 代码。

访问已知邮件文件夹

函数使用 userClient.Me().MailFolders.ByMailFolderId("inbox").Messages() 请求生成器,该生成器生成对 列表消息 API 的请求。 由于它包含 ByMailFolderId("inbox") 请求生成器,API 仅返回所请求邮件文件夹中的邮件。 在这种情况下,由于收件箱是用户邮箱中默认的已知文件夹,因此可通过其已知名称访问该文件夹。 非默认文件夹的访问方式相同,方法是将已知名称替换为邮件文件夹的 ID 属性。 有关可用已知文件夹名称的详细信息,请参阅 mailFolder 资源类型

访问集合

与上一 GetUser 部分中返回单个 对象的函数不同,此方法返回消息集合。 Microsoft Graph 中返回集合的大多数 API 不会在单个响应中返回所有可用结果。 相反,它们使用 分页 返回部分结果,同时为客户端提供请求下一页的方法。

默认页面大小

使用分页的 API 实现默认页面大小。 对于消息,默认值为 10。 客户端可以使用 $top 查询参数请求更多 (或更少的 ) 。 在 中GetInbox,使用查询参数中的 属性完成Top添加$top

注意

传入的值 Top 是上限,而不是显式数字。 API 返回一些 消息,最多返回 指定值。

获取后续页面

如果服务器上有更多可用的结果,则集合响应将包含一个 @odata.nextLink 具有 API URL 的属性,用于访问下一页。 Go SDK 在集合页对象上提供 GetOdataNextLink 方法。 如果此方法返回非 nil,则有更多可用的结果。

集合排序

函数使用 OrderBy 查询参数中的 属性请求按接收消息的时间排序的结果, (receivedDateTime 属性) 。 它包含DESC关键字 (keyword) 以便先列出最近收到的消息。 属性 OrderBy$orderby 查询参数 添加到 API 调用。

发送邮件

现在,添加以经过身份验证的用户身份发送电子邮件的功能。

  1. 将以下函数添加到 ./graphhelper/graphhelper.go

    func (g *GraphHelper) SendMail(subject *string, body *string, recipient *string) error {
        // Create a new message
        message := models.NewMessage()
        message.SetSubject(subject)
    
        messageBody := models.NewItemBody()
        messageBody.SetContent(body)
        contentType := models.TEXT_BODYTYPE
        messageBody.SetContentType(&contentType)
        message.SetBody(messageBody)
    
        toRecipient := models.NewRecipient()
        emailAddress := models.NewEmailAddress()
        emailAddress.SetAddress(recipient)
        toRecipient.SetEmailAddress(emailAddress)
        message.SetToRecipients([]models.Recipientable{
            toRecipient,
        })
    
        sendMailBody := users.NewItemSendMailPostRequestBody()
        sendMailBody.SetMessage(message)
    
        // Send the message
        return g.userClient.Me().SendMail().Post(context.Background(), sendMailBody, nil)
    }
    
  2. graphtutorial.go 中的空sendMail函数替换为以下内容。

    func sendMail(graphHelper *graphhelper.GraphHelper) {
        // Send mail to the signed-in user
        // Get the user for their email address
        user, err := graphHelper.GetUser()
        if err != nil {
            log.Panicf("Error getting user: %v", err)
        }
    
        // For Work/school accounts, email is in Mail property
        // Personal accounts, email is in UserPrincipalName
        email := user.GetMail()
        if email == nil {
            email = user.GetUserPrincipalName()
        }
    
        subject := "Testing Microsoft Graph"
        body := "Hello world!"
        err = graphHelper.SendMail(&subject, &body, email)
        if err != nil {
            log.Panicf("Error sending mail: %v", err)
        }
    
        fmt.Println("Mail sent.")
        fmt.Println()
    }
    
  3. 运行应用,登录并选择选项 3 以向自己发送电子邮件。

    Please choose one of the following options:
    0. Exit
    1. Display access token
    2. List my inbox
    3. Send mail
    4. Make a Graph call
    3
    Mail sent.
    

    注意

    如果使用 Microsoft 365 开发人员计划中的开发人员租户进行测试,则发送的电子邮件可能未送达,并且可能会收到无法交付的报告。 如果要取消阻止从租户发送邮件,请通过Microsoft 365 管理中心联系支持人员。

  4. 若要验证是否已收到邮件,请选择选项 2 列出收件箱。

SendMail 说明

请考虑 函数中的 SendMail 代码。

发送邮件

函数使用 userClient.Me().SendMail() 请求生成器,该生成器生成对 发送邮件 API 的请求。 请求生成器采用表示 Message 要发送的消息的 对象。

创建对象

与以前调用 Microsoft Graph(仅读取数据)不同,此调用会创建数据。 若要使用客户端库创建项,请创建表示在这种情况下的数据 (的 类的实例, models.Message) ,设置所需的属性,然后在 API 调用中发送它。 由于调用正在发送数据,因此使用 Post 方法而不是 Get

后续步骤