3

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);
    }
ANJYR
  • 2,583
  • 6
  • 39
  • 60
  • 3
    do you have code.. or anything that you have actually written or tried on your own.. here is a link that will help you get started [Find C# code examples and Solutions Easy](http://www.google.com) – MethodMan Sep 16 '14 at 05:03
  • basically i know how to send email through asp.net, i already done but my concern is how to create webapi, who send email through webapi? – ANJYR Sep 16 '14 at 05:11
  • `smtp.EnableSsl = true;` where are you using Credentials.. where are you setting and or reading the Port. also wrap the smtp.Send(mm) around a try{}catch and see if you are getting any error messages returned..? this is not that difficult – MethodMan Sep 16 '14 at 05:21
  • i use credential in web config. all these code covered by try catch block. i just provide you logic. – ANJYR Sep 16 '14 at 05:25
  • 3
    This is the first hit on google when searching `send email from webapi` – crthompson Jul 20 '16 at 21:27

2 Answers2

6

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

atom.gregg
  • 987
  • 8
  • 14
  • can you provide me sample of code – ANJYR Sep 16 '14 at 05:27
  • I just noticed you mentioned you don't know too much about Web API. Check out the documentation.. It is extremely straight forward and putting the two together is all you need. Remember KISS. – atom.gregg Sep 16 '14 at 05:27
  • 1
    @Anjyr, I added links to the documentation. I think it's probably best you work through that to flesh out your knowledge of both technologies before copying/pasting code you may not understand. Also, please make sure you secure the service, otherwise you might bombard a lot of us with emails from hackers who have discovered your un-secured email server. – atom.gregg Sep 16 '14 at 05:31
6
    # Call this function in your WebApi controller #
=========================================================
        private void sendEmailViaWebApi()
        {
            string subject = "Email Subject";
            string body = "Email body";
            string FromMail = "shahid@reckonbits.com.pk";
            string emailTo = "reciever@reckonbits.com.pk";
            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("shahid@reckonbits.com.pk", "your password");
            SmtpServer.EnableSsl = false;
            SmtpServer.Send(mail);
        }
Falcon
  • 79
  • 1
  • 10
Shahid Ullah
  • 153
  • 2
  • 6