0

I am getting an exception 'The value cannot be null or empty. (Parameter 'mediaType')' when trying to send an attachment using the RestSharp library. For email sending, I am using MailGun api and the solution is on .NET 7 framework

var client = new RestClient(new Uri(BaseUrl))
{
    `Authenticator = new HttpBasicAuthenticator("api", ApiKey)
};
                    
var request = new RestRequest();
request.AddParameter("domain", domain, ParameterType.UrlSegment);
request.Resource = $"{domain}/messages";
request.AddParameter("from", "noreply@smartbroker.com");
request.AddParameter("to", "ahuja.dinesh85@gmail.com");
request.AddParameter("subject", "test subject");
request.AddParameter("html", "hello");
    
//Attachment
const string fileName = "D:\\ebook.pdf";                
request.AddFile("attachment", fileName);
    
request.Method = Method.Post;
    
var response = await client.ExecuteAsync(request);

If I comment the attachment code the email goes fine. Do you have any idea what I am doing wrong?

Thanks in advance

Dinesh Ahuja
  • 417
  • 1
  • 5
  • 11

3 Answers3

0

It seems there is a problem with the version of RestSharp. I couldn't fix that problem, but it works fine on .net 6 and earlier.

I hope it has helped you, greetings!

0

I also have working .net 6 code failing after upgrading a Maui project using RestSharp to .net 7. Now I can only post parameters or a file, but not longer both together. After a lot of searching I have found these :

https://github.com/dotnet/runtime/issues/81506

https://github.com/restsharp/RestSharp/issues/1976

So I am guessing there is no solution until either Microsoft fixes this bug or RestSharp come up with a workaround.

  • I have tried the suggested V109 pre-release and the problem has now been fixed. – Stephen Winstanley Mar 07 '23 at 13:21
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '23 at 01:53
0

I faced same problem but i discover that the problem was in restsharp version. now if you use latest restSharp version it will solved without any intervention. the latest version is 109.0.1.