Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email through Asp.Net Web Api [duplicate]

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);
    }
like image 476
ANJYR Avatar asked Apr 30 '26 13:04

ANJYR


2 Answers

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

like image 187
atom.gregg Avatar answered May 03 '26 01:05

atom.gregg


    # 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);
        }
like image 38
Shahid Ullah Avatar answered May 03 '26 02:05

Shahid Ullah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!