Graph api send mail with attachment from SharePoint Online:
Alternative:
This Graph api has 4MB total size limitation over which you can go for ShareLink option using [/_api/web/Lists/getByTitle('Documents')/GetItemById('1')/ShareLink] rest api with minimum 'Members' permission level.
Preq:
Need to Register App in Graph and give it 'Admin Consent'.
Graph api Encoded Code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
<script type="text/javascript">
$(document).ready(function() {
requestToken();
});
var token;
function requestToken() {
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/XXX.onmicrosoft.com/oauth2/v2.0/token",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "client_credentials",
"client_id ": "XXX",
"client_secret": "XXX",
"scope ": "https://graph.microsoft.com/.default"
},
success: function(response) {
token = response.access_token;
},
error: function(error) {
console.log(JSON.stringify(error));
}
})
}
function sendEmail() {
//Variable to get values from input elements
var to = $('#email').val();
var sub = $('#txtsubject').val();
var msg = $('#txtmessage').val();
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://graph.microsoft.com/v1.0/users/XXX@XXX.onmicrosoft.com/sendMail",
"method": "POST",
"headers": {
"authorization":"Bearer " + token,
"content-type": "application/json"
},
"processData": false,
"data": JSON.stringify({
"message": {
"subject": sub,
"body": {
"contentType": "Text",
"content": msg
},
"toRecipients": [{
"emailAddress": {
"address": to
}
}]
}
}),
success: function(response) {
alert("email sent successfully to " + to + "");
},
error: function(error) {
console.log(JSON.stringify(error));
alert(JSON.stringify(error));
}
})
}
</script>
<div class="container">
<div class="form-group">
<label for="email">To:</label>
<input type="email" class="form-control" id="email" required>
</div>
<div class="form-group">
<label for="txtsubject">Subject:</label>
<input type="text" class="form-control" id="txtsubject" required>
</div>
<div class="form-group">
<label for="txtmessage">Message:</label>
<textarea class="form-control" id="txtmessage" rows="7" required></textarea>
</div>
<button type="submit" onclick="sendEmail();" class="btn btn-default">Send</button>
</div>
</body>
ShareLink Encoded Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
getShareLink();
});
function getShareLink(){
var formDigestURL ="https://XXX/_api/contextinfo";
return $.ajax({
url: formDigestURL,
type: "POST",
datatype:"json",
headers: {
"accept": "application/json;odata=verbose",
},
success:function(formDigestData){
var dataURL="https://XXX/_api/web/Lists/getByTitle('Documents')/GetItemById('1')/ShareLink";
$.ajax({
url: dataURL,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": formDigestData.d.GetContextWebInformation.FormDigestValue,
},
contentType: "application/json; charset=utf-8",
dataType: "json",
data:JSON.stringify({
"request":{
"createLink":true,
"settings":{
"allowAnonymousAccess":false,
"linkKind":2,
"expiration":null,
"restrictShareMembership":false,
"updatePassword":false,
"password":""
}
}
}),
success:function(data){
alert("Sucess");
console.log(data.d.ShareLink.sharingLinkInfo.Url);
$('Div#showLinks').html(data.d.ShareLink.sharingLinkInfo.Url);
},
error: function (error) {
alert("Error");
console.log(JSON.stringify(error));
}
});
},
error: function (error) {
$("#error").append(JSON.stringify(error));
}
});
}
</script>
<div id="showLinks"></div>
Alternative:
This Graph api has 4MB total size limitation over which you can go for ShareLink option using [/_api/web/Lists/getByTitle('Documents')/GetItemById('1')/ShareLink] rest api with minimum 'Members' permission level.
Preq:
Need to Register App in Graph and give it 'Admin Consent'.
Graph api Encoded Code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
<script type="text/javascript">
$(document).ready(function() {
requestToken();
});
var token;
function requestToken() {
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/XXX.onmicrosoft.com/oauth2/v2.0/token",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "client_credentials",
"client_id ": "XXX",
"client_secret": "XXX",
"scope ": "https://graph.microsoft.com/.default"
},
success: function(response) {
token = response.access_token;
},
error: function(error) {
console.log(JSON.stringify(error));
}
})
}
function sendEmail() {
//Variable to get values from input elements
var to = $('#email').val();
var sub = $('#txtsubject').val();
var msg = $('#txtmessage').val();
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://graph.microsoft.com/v1.0/users/XXX@XXX.onmicrosoft.com/sendMail",
"method": "POST",
"headers": {
"authorization":"Bearer " + token,
"content-type": "application/json"
},
"processData": false,
"data": JSON.stringify({
"message": {
"subject": sub,
"body": {
"contentType": "Text",
"content": msg
},
"toRecipients": [{
"emailAddress": {
"address": to
}
}]
}
}),
success: function(response) {
alert("email sent successfully to " + to + "");
},
error: function(error) {
console.log(JSON.stringify(error));
alert(JSON.stringify(error));
}
})
}
</script>
<div class="container">
<div class="form-group">
<label for="email">To:</label>
<input type="email" class="form-control" id="email" required>
</div>
<div class="form-group">
<label for="txtsubject">Subject:</label>
<input type="text" class="form-control" id="txtsubject" required>
</div>
<div class="form-group">
<label for="txtmessage">Message:</label>
<textarea class="form-control" id="txtmessage" rows="7" required></textarea>
</div>
<button type="submit" onclick="sendEmail();" class="btn btn-default">Send</button>
</div>
</body>
ShareLink Encoded Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
getShareLink();
});
function getShareLink(){
var formDigestURL ="https://XXX/_api/contextinfo";
return $.ajax({
url: formDigestURL,
type: "POST",
datatype:"json",
headers: {
"accept": "application/json;odata=verbose",
},
success:function(formDigestData){
var dataURL="https://XXX/_api/web/Lists/getByTitle('Documents')/GetItemById('1')/ShareLink";
$.ajax({
url: dataURL,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": formDigestData.d.GetContextWebInformation.FormDigestValue,
},
contentType: "application/json; charset=utf-8",
dataType: "json",
data:JSON.stringify({
"request":{
"createLink":true,
"settings":{
"allowAnonymousAccess":false,
"linkKind":2,
"expiration":null,
"restrictShareMembership":false,
"updatePassword":false,
"password":""
}
}
}),
success:function(data){
alert("Sucess");
console.log(data.d.ShareLink.sharingLinkInfo.Url);
$('Div#showLinks').html(data.d.ShareLink.sharingLinkInfo.Url);
},
error: function (error) {
alert("Error");
console.log(JSON.stringify(error));
}
});
},
error: function (error) {
$("#error").append(JSON.stringify(error));
}
});
}
</script>
<div id="showLinks"></div>
BEST EVER IN THE WORLD. YOU MUST TRY THIS !
ReplyDeleteAMAZING WORK GUYS JUST KEEP IT UP !
SITE IS HERE :Best PDO Threads in Gig Harbor
Thanks for sharing such a great information.. It really helpful to me..I always search to read the quality content and finally i found this in you post. keep it up!
ReplyDeleteDigital Marketing Course in Raipur
Your Blog is very nice.
ReplyDeleteWish to see this much more like this. Thanks for sharing your information!
Best Eye Hospital in Ambikapur
I read this post your post so nice and very informative post thanks for sharing this post
ReplyDelete3 BHK Apartments in Vidhan Sabha Road, Raipur
Thanks for sharing this site bro. fantastic
ReplyDeleteBest Gynaecology Hospital in Bettiah
"This post was incredibly insightful! Your detailed explanation on the topic has cleared up a lot of confusion for me. Thank you for sharing your expertise and making complex concepts easy to understand. Looking forward to more informative articles from you!"
ReplyDeleteModular Kitchen & Wardrobe in Raipur
Great post on leveraging the Graph API for sending emails with attachments in SharePoint Online! This practical guide is invaluable for developers aiming to enhance their SharePoint applications. For those interested in expanding their digital skills, consider enrolling in a Best Digital Marketing Course In Noida By Digiperform . Understanding digital marketing can complement technical skills like SharePoint development, providing a holistic approach to managing online content and reaching broader audiences. Keep sharing such informative content!
ReplyDelete