Shiv Kumar Yadav

Shiv Kumar Yadav

  • NA
  • 269
  • 25.7k

Connecting Xamarin with web api

Jun 8 2018 7:32 AM
My Webapi Code
 
  1. [HttpGet]  
  2. public HttpResponseMessage SendMail1(string tomail)  
  3. {  
  4.     bool responce = false;  
  5.     try  
  6.     {  
  7.         MailMessage mail = new MailMessage();  
  8.         SmtpClient smtpServer = new SmtpClient();  
  9.         string password = ConfigurationManager.AppSettings["password"];  
  10.         mail.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"]);  
  11.         //string[] toRecipients = vm.ToRecipients.Split(',');  
  12.         //foreach (string toRecipient in toRecipients)  
  13.         //{  
  14.         //    if (!string.IsNullOrEmpty(toRecipient))  
  15.         //        mail.To.Add(new MailAddress(toRecipient));  
  16.         //}  
  17.         mail.To.Add(new MailAddress(tomail));  
  18.         mail.Subject = "subject";  
  19.         mail.Body = "body txt";  
  20.         //vm.FromRecipients = ConfigurationManager.AppSettings["MailFrom"];  
  21.         smtpServer.Host = ConfigurationManager.AppSettings["Host"];  
  22.         smtpServer.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);  
  23.         smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;  
  24.         smtpServer.EnableSsl = bool.Parse(ConfigurationManager.AppSettings["EnableSsl"]);  
  25.         smtpServer.Timeout = 100000;  
  26.         smtpServer.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailFrom"], password);  
  27.         smtpServer.Send(mail);  
  28.   
  29.         var Result = this.Request.CreateResponse(HttpStatusCode.OK, responce, new JsonMediaTypeFormatter());  
  30.   
  31.         return Result;  
  32.     }  
  33.     catch (Exception ex)  
  34.     {  
  35.         HttpError Error = new HttpError(ex.Message) { { "IsSuccess"false } };  
  36.         return this.Request.CreateErrorResponse(HttpStatusCode.OK, Error);  
  37.     }  
  38.   
  39. }  
This is my Xamarin Code
 
  1. private async void btnLogin_Clicked(object sender, EventArgs e)  
  2.   
  3.    try  
  4.    {  
  5.        EmailConfigDetailsVM emildetails = new EmailConfigDetailsVM();  
  6.        HttpClient client = new HttpClient();  
  7.        string apiUrl = "http://localhost:52869/api/default/sendmail1?tomail=";  
  8.        emildetails.ToRecipients ="*********@gmail.com";  
  9.        emildetails.Body ="test";  
  10.        emildetails.Subject = "test";  
  11.        var serializedProduct = JsonConvert.SerializeObject(emildetails);  
  12.        var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json");  
  13.        HttpResponseMessage response = client.GetAsync(apiUrl+ "*********@gmail.com").Result;  
  14.        if (response.IsSuccessStatusCode)  
  15.        {  
  16.            DisplayAlert("Alert""Send Successfully""OK");  
  17.        }  
  18.        else  
  19.        {  
  20.            DisplayAlert("Alert""Send Failed...""OK");  
  21.        }  
  22.    }  
  23.    catch (Exception ex)  
  24.    {  
  25.        string err = ex.InnerException.ToString();  
  26.        errormsg.Text = ex.Message.ToString();  
  27.    }  
 it throw an exception in xamrin: Exception has been thrown by the target of an invocation.
 
Please help me.

Answers (1)