hack-together icon indicating copy to clipboard operation
hack-together copied to clipboard

Project: Displaying personal info and sending emails via Microsoft Graph

Open ron-zhong opened this issue 1 year ago • 8 comments

Project name

Displaying personal info and sending emails via Microsoft Graph

Description

Try it out here https://blazor-graph.azurewebsites.net Untitled video - Made with Clipchamp (3)

By participating Hack Together: Microsoft Graph and .NET hackathon as beginners, we downloaded the sample code from here

We created a new app registration Hackathon.Graph.Blazor under our coporate POC Azure subscription and configured the post-login-return-url as below: image

We enriched the sample application by retrieving additional personal info and display first 10 email subjects via Microsoft Graph .

1. Display personal info like: name, mobile number, job title, and etc

<table class="table table-striped table-condensed" style="font-family: monospace">
    <tr>
        <th>Property</th>
        <th>Value</th>
    </tr>
    <tr>
        <td>Name</td>
        <td>@_user.DisplayName</td>
    </tr>
    <tr>
        <td>Mail</td>
        <td>@_user.Mail</td>
    </tr>
    <tr>
        <td>MobilePhone</td>
        <td>@_user.MobilePhone</td>
    </tr>
    <tr>
        <td>OfficeLocation</td>
        <td>@_user.OfficeLocation</td>
    </tr>
    <tr>
        <td>JobTitle</td>
        <td>@_user.JobTitle</td>
    </tr>
</table>

2. Displaying first 25 email messages

<h4 class="text-primary">Top 25 Emails</h4>
<div class="table-responsive">
    <table class="table table-hover table-borderless">
    <thead>
        <tr>
            <td>From</td>
            <td>Subject</td>
            <td>Received at</td>
        </tr>
    </thead>
    <tbody>
        @foreach (var message in _messages)
        {
            <tr>
                <td class="text-primary text-decoration-underline">@(message.From.EmailAddress.Address)</td>
                <td>@message.Subject</td>
                <td class="text-nowrap">@(message.ReceivedDateTime.Value.ToLocalTime().ToString())</td>

            </tr>
        }
    </tbody>
</table>
</div>

image

3. Send an email via Microsoft Graph

var message = new Message
{
    Subject = subject,
    Body = new ItemBody
    {
        Content = body,
        ContentType = BodyType.Text
    },
    ToRecipients = new Recipient[]
    {
        new Recipient
        {
            EmailAddress = new EmailAddress
            {
                Address = to
            }
        }
    }
};
await _graphServiceClient.Me.SendMail(message).Request().PostAsync();

image

Thank you very much!! Happy coding!!!

Contributors

Ron Zhong Su Su Mirza Ghulam Rasyid Marxis Cabero

Repo URL

https://github.com/mimshub/hack-together-graph-blazor-net7

Team members

ron-zhong, mims-susu, mirzaevolution, mccabero

ron-zhong avatar Mar 15 '23 07:03 ron-zhong