question

AneeshAS-7702 avatar image
0 Votes"
AneeshAS-7702 asked AneeshAS-7702 answered

How can we send email from microsft graph api to third party email providers like gmail/yahoo using asp.net

Hi,
How can we send email from microsoft graph api to third party email providers like gmail/yahoo using asp.net

microsoft-graph-mail
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

while sending the email from graph api, i got the below message.

Code: ResourceNotFound
Message: Resource could not be discovered.
Inner error:

0 Votes 0 ·
Danstan-MSFT avatar image
0 Votes"
Danstan-MSFT answered AneeshAS-7702 commented

First, please add the code or request payload you are using to send mail and include the inner error details. You error is 404 and seems to be because you are using an account that does not have a mailbox.

As for sending mail from Graph to other third party providers, you just need to use MS Graph SendMail and add the emails in the payload. Here is a sample I have used to send mail to Gmail from Exchange.

 POST https://graph.microsoft.com/v1.0/me/sendMail
 {
     "message": {
         "subject": "Meet for lunch?",
         "body": {
             "contentType": "Text",
             "content": "The new cafeteria is open."
         },
         "toRecipients": [
             {
                 "emailAddress": {
                     "address": "myname@otherdomain.com"
                 }
             }
         ],
         "ccRecipients": [
             {
                 "emailAddress": {
                     "address": "myname@gmail.com"
                 }
             }
         ]
     },
     "saveToSentItems": "false"
 }

But as I said, your problem seems to be with an account that does not have a mail box. Try again and share the details requested.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

First of all thanks for your reply.
Its code is working fine in using the windows account(.onmicrosoft.com).
public static async Task SendMail(string userEmail)
{userEmail ="aneesh@gmail.com";
GraphServiceClient graphClient;
AzureGraphAuthConfig config = AzureGraphAuthConfig.ReadFromJsonFile("appsettings.json");
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(config.GraphClientId)
.WithTenantId(config.GraphTenant)
.WithClientSecret(config.GraphClientSecret)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
graphClient = new GraphServiceClient(authProvider);
var message = new Message
{
Subject = "Meet for lunch?",
Body = new ItemBody
{
ContentType = BodyType.Text, Content = "The new cafeteria is open."
},
ToRecipients = new List<Recipient>()
{
new Recipient
{ EmailAddress = new EmailAddress
{ Address = userEmail }
}
}
};
await graphClient.Users[userEmail].SendMail(message, false).Request().PostAsync();
}

0 Votes 0 ·

Errors as follows,

Code: ResourceNotFound
Message: Resource could not be discovered.
Inner error:
AdditionalData:
date: 2021-09-02T09:51:24
request-id:
client-request-id:
ClientRequestId:



{"error":{"code":"ResourceNotFound","message":"Resource could not be discovered.","innerError":{"date":"2021-09-02T09:51:24","request-id":"44444","client-request-id":"666"}}}

0 Votes 0 ·
AneeshAS-7702 avatar image
0 Votes"
AneeshAS-7702 answered

hi,
I have attached the permissions, which is applied in the account.

132411-permissions.png



permissions.png (78.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.