3

I'm quite new to MVC and Web API. I'm trying to post a MailMessage(System.Net.Mail) object to the web api that I've created but the object is received as empty at API. I'm using ReshSharp to call the api below is my code:

 MailMessage myMail = new System.Net.Mail.MailMessage("from@example.com", "to@example.com");
 myMail.Subject = "Test message from client";
 myMail.SubjectEncoding = System.Text.Encoding.UTF8;
 myMail.Body = "<b>Test Mail</b><br>using <b>HTML from client</b>.";
 myMail.BodyEncoding = System.Text.Encoding.UTF8;
 myMail.IsBodyHtml = true;

 RestClient client = new RestClient("http://localhost:53014/api/email");
 var request = new RestRequest("SendMailMessage", Method.POST);
 var json = request.JsonSerializer.Serialize(myMail);
 request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
 request.RequestFormat = DataFormat.Json;
 var res = client.Execute(request);

This is my API method:

[HttpPost]
public void SendMailMessage([FromBody]MailMessage myEmail)
{
     //Code to send email
}

This is what I receive at the API end: API Post method

I have also tried this way but same output:

request = new RestRequest("SendMailMessage", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(myMail);
res = client.Execute(request);

I have also tried the Newtonsoft.Json serializer to serialize object but no success var json = JsonConvert.SerializeObject(myMail);

Can anyone suggest what am I missing here?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Rakesh
  • 51
  • 4
  • Create a custom model/viewmodel/payload to hold the information you want sent to the action and then construct the mail message in the action using the properties provided to the action. Trying to send a MailMessage is problematic. – Nkosi Jan 16 '17 at 13:00

1 Answers1

0

Trying to send a MailMessage is problematic.

You should create a custom object to hold the information you want sent to the web API...

public class Email {
    public string Body { get; set; }        
    public bool IsBodyHtml { get; set; }
    public string Subject { get; set; }
    public string[] To { get; set; }
    //...Any other properties you deem relevant
    //eg: public string From { get; set; }
}

Keep it simple.

So now you would send the custom object to the web api

 var myMail = new Email() {
     To = new [] { "to@example.com" },
     Subject = "Test message from client",
     Body = "<b>Test Mail</b><br>using <b>HTML from client</b>.",
     IsBodyHtml = true
 };     

 var client = new RestClient("http://localhost:53014/api/email");
 var request = new RestRequest("SendMailMessage", Method.POST);
 var json = request.JsonSerializer.Serialize(myMail);
 request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
 request.RequestFormat = DataFormat.Json;
 var res = client.Execute(request);

Then construct the mail message in the action using the properties provided to the action.

public class EmailController : ApiController {

    [HttpPost]
    public async Task<IHttpActionResult> SendMailMessage([FromBody]Email message) {

        if (ModelState.IsValid) {
            var myMail = new System.Net.Mail.MailMessage();
            myMail.Subject = message.Subject;
            myMail.SubjectEncoding = System.Text.Encoding.UTF8;
            myMail.Body = message.Body;
            myMail.BodyEncoding = System.Text.Encoding.UTF8;
            myMail.IsBodyHtml = message.IsBodyHtml;
            myMail.From = new MailAddress("from@example.com");

            foreach (var to in message.To) {
                myMail.To.Add(to);
            }

            //...Code to send email
        }
        return BadRequest(ModelState);
    }

}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Why is it problematic, is it since MailMessage is not serializable? Building a custom type was the last way I was thinking is it the only way? – Rakesh Jan 17 '17 at 04:58
  • I think I will go this way only, but I cannot mark this as answer. – Rakesh Jan 18 '17 at 09:28