My client want to send E-mail through asp.net Web API. I am new in web services and have 0 knowledge of this please guide me how to achieve this task and if anyone can provide the code, there must be some web service available like this.
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
The .NET mail API sends emails via SMTP. The ASP.NET Web API allows you to host a REST interface.
You said you know how to program with both, so do that.
Create your Web API method which receives the necessary arguments, exactly like you would for a normal method that will do your emailing duties for you. Then create your route to the method.
If you have issues with either not working, then write tests.
Documentation for Web API: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
Documentation for SmtpClient: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx
# Call this function in your WebApi controller #
=========================================================
private void sendEmailViaWebApi()
{
string subject = "Email Subject";
string body = "Email body";
string FromMail = "[email protected]";
string emailTo = "[email protected]";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("mail.reckonbits.com.pk");
mail.From = new MailAddress(FromMail);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "your password");
SmtpServer.EnableSsl = false;
SmtpServer.Send(mail);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With