TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Shiv Kumar Yadav
NA
269
25.7k
Connecting Xamarin with web api
Jun 8 2018 7:32 AM
My Webapi Code
[HttpGet]
public
HttpResponseMessage SendMail1(
string
tomail)
{
bool
responce =
false
;
try
{
MailMessage mail =
new
MailMessage();
SmtpClient smtpServer =
new
SmtpClient();
string
password = ConfigurationManager.AppSettings[
"password"
];
mail.From =
new
MailAddress(ConfigurationManager.AppSettings[
"MailFrom"
]);
//string[] toRecipients = vm.ToRecipients.Split(',');
//foreach (string toRecipient in toRecipients)
//{
// if (!string.IsNullOrEmpty(toRecipient))
// mail.To.Add(new MailAddress(toRecipient));
//}
mail.To.Add(
new
MailAddress(tomail));
mail.Subject =
"subject"
;
mail.Body =
"body txt"
;
//vm.FromRecipients = ConfigurationManager.AppSettings["MailFrom"];
smtpServer.Host = ConfigurationManager.AppSettings[
"Host"
];
smtpServer.Port =
int
.Parse(ConfigurationManager.AppSettings[
"Port"
]);
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.EnableSsl =
bool
.Parse(ConfigurationManager.AppSettings[
"EnableSsl"
]);
smtpServer.Timeout = 100000;
smtpServer.Credentials =
new
System.Net.NetworkCredential(ConfigurationManager.AppSettings[
"MailFrom"
], password);
smtpServer.Send(mail);
var Result =
this
.Request.CreateResponse(HttpStatusCode.OK, responce,
new
JsonMediaTypeFormatter());
return
Result;
}
catch
(Exception ex)
{
HttpError Error =
new
HttpError(ex.Message) { {
"IsSuccess"
,
false
} };
return
this
.Request.CreateErrorResponse(HttpStatusCode.OK, Error);
}
}
This is my Xamarin Code
private
async
void
btnLogin_Clicked(
object
sender, EventArgs e)
try
{
EmailConfigDetailsVM emildetails =
new
EmailConfigDetailsVM();
HttpClient client =
new
HttpClient();
string
apiUrl =
"http://localhost:52869/api/default/sendmail1?tomail="
;
emildetails.ToRecipients =
"*********@gmail.com"
;
emildetails.Body =
"test"
;
emildetails.Subject =
"test"
;
var serializedProduct = JsonConvert.SerializeObject(emildetails);
var content =
new
StringContent(serializedProduct, Encoding.UTF8,
"application/json"
);
HttpResponseMessage response = client.GetAsync(apiUrl+
"*********@gmail.com"
).Result;
if
(response.IsSuccessStatusCode)
{
DisplayAlert(
"Alert"
,
"Send Successfully"
,
"OK"
);
}
else
{
DisplayAlert(
"Alert"
,
"Send Failed..."
,
"OK"
);
}
}
catch
(Exception ex)
{
string
err = ex.InnerException.ToString();
errormsg.Text = ex.Message.ToString();
}
it throw an exception in xamrin:
Exception has been thrown by the target of an invocation.
Please help me.
Reply
Answers (
1
)
How can I show a graphical interface in Xamarin.Forms?
How to SQLite Local DB Connection in Xamarin