Using C# WPF to send pre-generated emails w/attachments via the GRAPH API. We've been successful when sending emails before with an attachment that is about 100kb, but upon expanding our capabilities to three attachments we're getting Error 413 responses from GRAPH.
The attachments on the new message receiving the error are:
1 PDF @ 2 mb, 1 PDF @ 20 mb, 1 CSV @ 100 kb
Our sending code is fairly simple:
public void SendEmail(string messageJson, string token)
{
HttpWebRequest request = WebRequest.Create($"https://graph.microsoft.com/v1.0/me/sendMail") as HttpWebRequest;
request.ContentType = "application/json";
request.Method = "POST";
request.Accept = ("application/json");
request.Headers.Add("Authorization", $"Bearer {token}");
string resultJSON = string.Empty;
try
{
//Send Request
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(messageJson);
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
resultJSON = reader.ReadToEnd();
}
}
}
catch (WebException e)
{
var response = ((HttpWebResponse)e.Response);
var someheader = response.Headers["X-API-ERROR"];
// check header
string content = string.Empty;
// protocol errors find the statuscode in the Response
// the enum statuscode can be cast to an int.
int code = (int)((HttpWebResponse)e.Response).StatusCode;
using (var reader = new StreamReader(e.Response.GetResponseStream()))
{
content = reader.ReadToEnd();
}
Console.WriteLine(content);
}
}
All of the solutions I have found are referring to WCF and require changing a web.config file, a file which our application does not contain.
We have attempted setting the content length, setting the request to SendChunked, but neither has worked. Documentation for GRAPH states that the maximum attachment size is 150 mb, which our messages are well under.
As per the doc , If the file size is between 3 MB and 150 MB, its recommended to create an upload session using PUT request in place of POST , POST is generally for the file size under 3 MB, Check the doc for more info. .
Could you please using PUT in place of POST.
Hope this help
Thanks