how to send a email using office 365 api in c#.net? - office365api

how to send a email using office 365 API in c#.net?can anyone provide a sample code. I tried with Office 365 Exchange Web serivce and its worked.

It is not clear if you are looking for samples using SDK or raw HTTP requests.
Samples for sending mail using raw HTTP requests in C# are here and here. Below is sample code snippet to send mail using the sdk:
GraphServiceClient client = new GraphServiceClient(<UseYourAuthProvider>);
Message message = new Message
{
Subject="Hello",
Body=new ItemBody{Content="World",ContentType=BodyType.Text},
ToRecipients=new []{new Recipient{EmailAddress=new EmailAddress{Address="john#contoso.com"}}}
};
var request = client.Me.SendMail(message, true);
await request.Request().PostAsync();
A complete sample using the sdk is available here (Note: it is not related to sending mail, but should give you reference to sdk usage in general)

Related

MS Graph Mail The specified object was not found in the store

I am trying to retrive information about mails within a users mailbox.
I am using the following snippet of code:
graphClient.Users[mailbox]
.Messages[messageId]
.Request()
.GetAsync()
.Result;
Which works fine for the most requests.
The messageId is read previously from the graph API.
The app registration has the following permissions.
Found this: using microsoft graph api to read mail behalf of users in the enterprise -> I'm using the application login method.
MessageId's change when an item is moved between folders so it most likely that is the reason your having the problem. To solve it use the immutableid https://learn.microsoft.com/en-us/graph/outlook-immutable-id

My app is not able to send message to Teams channel using graph api

I am trying to send message to teams channel using graph api. I am able to send the messages using my personal(user) tokens but not able to do it using an app which I have registered in Azure portal. My app also got access to protected apis from Microsft Teams.
Attaching the screen shot of app permissions and code snippet on how I am getting tokens.
app permissions
Code snippet to get tokens
One thing I noticed, when I decoded the the tokens I am getting, that roles in tokens only shows application permissions("Teamwork.Migrate.All") and it doesn't show delegated permissions("ChannelMessage.Read.All","ChannelMessage.Send","Teamwork.Migrate.All").
Below are the error message I am getting.
While doing get request to https://graph.microsoft.com/v1.0/teams/{teams_id}/channels/{channel_id}/messages
one of 'ChannelMessage.Read.All' or 'ChannelMessage.Read.Group' when using resont. Roles on the request 'Teamwork.Migrate.All' and resource specific consent g request ''", 'innerError': {'date': '2021-07-04T14:27:41', 'request-id': '8c1f-e345bf15da7b', 'client-request-id': '8c1fffc2-c94d-4d5b-a81d-e345bf15da7b'}}}
While doing post request to https://graph.microsoft.com/v1.0/teams/{teams_id}/channels/{channel_id}/messages/{message-id}/replies
{'error': {'code': 'Unauthorized', 'message': 'Unauthorized', 'innerError': {'d4:26:47', 'request-id': '44fe99be-1ed8-4505-a3e7-a88ce934e3f8', 'client-request8-4505-a3e7-a88ce934e3f8'}}}
You are currently using Get access without a user(Application permission). Application permissions are only supported for migration. Instead you should try delegated flow: Get access on behalf of a user. Please go though the documentation and you can try it using Postman or Fiddler.
You can also try Get started with Microsoft Graph and Python tutorial which shows fetching calendar information using Graph APIs.

Sending an MS Teams message using the Microsoft Graph API or BOT API

I'm trying to implement the following solution: a web application that subscribes to all MS teams chat messages. If a message contains forbidden text, the application should somehow warn the user (Ideally by replying to the same message, or, if not possible, initiate a conversation with the user).
I'm able to receive all chat webhooks and process them, but I could not find any way to post a message back to the Teams channel using the Graph API (the operation described in https://learn.microsoft.com/en-us/graph/api/channel-post-messagereply?view=graph-rest-beta&tabs=http
is not supported for Application permissions - only delegated ones which isn't suitable for our case).
So I'm trying to send proactive messages using the Bot framework, However, the bot framework requires a teams conversation ID which I don't have (the graph API webhook provides the team, channel and user IDs, none of which are accepted by the Bot API).
Does anyone know of a way I can retrieve the teams conversation ID using the team ID and channel ID provided by the graph API?
Thanks,
Dan
ConversationId for channel messages are combination of channelId and messageId both can be found in payload you get webhook notification. You can reply to existing conversation by using following by building converstionId like this:
conversationId = $"{channelId};messageid={messageId}"
For reply to work, your Bot needs to installed in the team and should have serviceURL saved at some place to refer back. Here is sample code which show how you can reply to existing message.
var serviceURL = "YOUR Service URL- You get this in each bot payload";
MicrosoftAppCredentials.TrustServiceUrl(serviceURL, DateTime.MaxValue);
using var connector = new ConnectorClient(new Uri(serviceURL, MicrosoftAppId, MicrosoftAppPassword);
var conversationId = $"{channelId};messageid={messageId}";
var replyActivity = MessageFactory.Text($"This is simple reply to existing conversation.");
replyActivity.Conversation = new ConversationAccount(id: conversationId);
var response = await connector.Conversations.SendToConversationAsync(conversationId, replyActivity);
For 1:1 reply- please take a look at Sending Proactive Message documentation.

Microsoft Graph API get message returns empty string

I am querying to get a single message from the Microsoft graph API like the following:
https://graph.microsoft.com/v1.0/users/<name>/messages/<id>
However, i am getting a response that is just an empty string. If i make the same request using the beta version of the api like the following:
https://graph.microsoft.com/beta/users/<name>/messages/<id>
The email in question is a calendar share invitation of content-type of "application/ms-tnef" and content-class of "Sharing" in the email headers.
I can't find any documentation indicating this is a known issue in the system. Is there any way to get this to work in the graph API or is the only work-around is to use the beta version instead?
Using Graph Explorer, and recreating your request using their demo accounts returns a result in v1.0. I assumed by name you meant the user's email address and I made the same request using the Guid id of the user.
https://graph.microsoft.com/v1.0/users/MeganB#M365x214355.onmicrosoft.com/messages/AAMkAGVmMDEzMTM4LTZmYWUtNDdkNC1hMDZiLTU1OGY5OTZhYmY4OABGAAAAAAAiQ8W967B7TKBjgx9rVEURBwAiIsqMbYjsT5e-T7KzowPTAAAAAAEMAAAiIsqMbYjsT5e-T7KzowPTAAHi4GJzAAA=
https://graph.microsoft.com/v1.0/users/48d31887-5fad-4d73-a9f5-3c356e68a038/messages/AAMkAGVmMDEzMTM4LTZmYWUtNDdkNC1hMDZiLTU1OGY5OTZhYmY4OABGAAAAAAAiQ8W967B7TKBjgx9rVEURBwAiIsqMbYjsT5e-T7KzowPTAAAAAAEMAAAiIsqMbYjsT5e-T7KzowPTAAIgOnGGAAA=
Have you attempted the same request using graph explorer?

Provide my users with a realtime API using Pusher

I'm using Pusher to add realtime functionality to my app. Is there a way to provide my users with realtime functionality through an API? I'm using private and presence channels, so connections to these need to be authenticated. Has anyone worked with Pusher and provided some sort of API to their users?
I'm doing this using Rails 3.1.
The solution here is to give the users you want to be able to access your data your app_key (not app_secret). They can then connect to Pusher and try to subscribe to your channels. They'll need to use JSONP authentication which makes a call to your server where you can authenticate the request to the private or presence channels.
Pusher.channel_auth_endpoint = 'http://yourserver.com/pusher_jsonp_auth';
Pusher.channel_auth_transport = 'jsonp';
var pusher = new Pusher('YOUR_APP_KEY');
var channel = pusher.subscribe('private-your-channel');
channel.bind('your_event', function(data) {
// do something here with data
});
In your authentication you'll need to check the referrer (domain) to see if you've given them access to your data along with what they are subscribing to.
You could also wrap this JavaScript up in your own library so that a subscription_error (authentication error) disconnects the client from Pusher.
Hope this helps. You can always drop an email to support#pusher.com too.

Resources