Monday, March 7, 2011

Writing, Deploying and Consuming a Web service in .NET

Suppose you want to write a web service that can send email and consume or use this web service in you C# application.
First off with writing the web service. For a detailed and basic version of the tutorial on how to write a web service, you can visit the following link.

Tutorial: Writing a web service

So we'll start by writing a web method that can get email address of the receiver, Subject of the email and Message as parameters and returns a message whether the email has been sent or not.
Here is what we'll need for the implementation of such a method.

[WebMethod]
public string sendMail(string mailTo, string emailSubject, string emailBody) {
MailMessage nmail = new MailMessage(); //Create a new mail message
nmail.From = new MailAddress(/*Your email address*/); //Set Sender's email address
nmail.To.Add(mailTo); //Set Recipient's Email address
//Note: you can add multiple Recipients
nmail.Subject = emailSubject; //Set subject of the email
nmail.Body = emailBody; //Set the body of the email
SmtpClient smtpClient = new SmtpClient(/*"127.0.0.1" or localhost or the IP or your server */); //Initiate SMTP client for your Server
try{
smtpClient.Send(nmail); //Send Email
}
catch(Exception e)
{
return “Error Occured”;
}
return "Mail Sent";
}

Also you will need the following dlls at the start of your code to make the above code executable

using System.Net.Mail;
using System.Web.Services.Protocols;

After this compile your code and see if it works fine, next step is to deploy your web service. Following tutorial describes how to deploy your web service.

Tutorial: How to Deploy your web service

Now once you have deployed your web service by the use of above tutorial. Next step is to consume it using your C# Forms application. So now open a new Visual C# Windows Application project. Make the form design as an email form with the labels of to, subject, message and a button which when pressed tries to send the email. My form looks like this.

Now you have to add the web reference to your application as shown in the figure below.

Right-Click References in the Solution Explorer and click Add references.Click on the link web service on the Local machine. This will display all the web services that are currently deployed on your IIS.


Select the one you just made and give it a reference name. In my case, I have given it a name “serv”. Click add reference.

It will appear under your refernces as

Now to the handler of the send button, you will have to initialize your service with the name you gave while adding reference. Like this

serv.Service snd = new sendemail.serv.Service();
Form the object snd you can call all the methods that are tagged as [web method]
String response=snd.sendmail(textBox1.Text, "", textBox3.Text, textBox4.Text);

You can show the string message returned by the service in the message box as follows

MessageBox.Show(response);

No comments:

Post a Comment

Please comment, if you like the post, you find any mistake or if you have any query.