1

I have the web service:

[WebMethod]
public void SendMail(string _name, string _email, string _message)
{
    //Create Mail Message Object with content that you want to send with mail.
    MailMessage MyMailMessage = new MailMessage("[email protected]", "[email protected]", "This is the mail subject", "Just wanted to say Hello");

    MyMailMessage.IsBodyHtml = false;

    //Proper Authentication Details need to be passed when sending email from gmail
    NetworkCredential mailAuthentication = new NetworkCredential("[email protected]", "xxxxxxxxx");

    //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
    //For different server like yahoo this details changes and you can
    //get it from respective server.
   SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

    //Enable SSL
    mailClient.EnableSsl = true;
    mailClient.UseDefaultCredentials = false;
    mailClient.Credentials = mailAuthentication;

    mailClient.Send(MyMailMessage);
}

and i have a html page. How can i call this function from my html page? (This function must send the message to email)

3
  • possible duplicate of $.ajax and webmethod/pagemethods Commented Sep 1, 2011 at 12:52
  • Are you willing to add the jQuery library? Commented Sep 1, 2011 at 12:54
  • 1
    @Mr. Disappointment - I don't see this as a duplicate of THAT page. Commented Sep 1, 2011 at 12:55

5 Answers 5

5

Use jQuery library. It makes ajax calls piece a cake. Then follow these items:

  1. Add an HTML button to your page, so that people can start the ajax process by clicking it
  2. Use jQuery to hook into the click event of that button (or span, or a div, or anything else)
  3. Make sure you have ScriptService attribute on your web service (this attribute means that you can call your service from JavaScript)
  4. Send items to your web service method

     $('#buttonId').click(function(){
         // Validating input
         $.ajax({
            type: 'POST',
            url: '/your-web-service-path.asmx/your-method-name',
            data: {} 
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function(r){},
            error: function(e){}
         });
    });
    

Just note that you have to make a JSON object out of your parameters and the name of the JSON properties should match the name of the web service parameters. Also note that the return value of the web service would be available to you as r.d object passed to success callback of ajax call.

Sign up to request clarification or add additional context in comments.

3 Comments

how can i post the parameters:(string _name, string _email, string _message) >?
create this object: {string_name: 'value1', string_email: 'value2', string_message: 'value3'}.
@_glebati: see where data:{}? You can put:data: {'_message':'hello world','_name':'john smith','_email':'[email protected]' }. The important thing is that your parameter names in the web service match the names on the data parameter on the javascript side and that the data parameter is valid JSON format.
0

If it's an aspx page, you can add a ScriptManager with EnablePageMethods="true"attribute, and then call PageMethods.sendMail(name, email, message, onSuccess, onFail);

1 Comment

it is simle html page with javascript
0

Maybe you want to take a look at jQuery's ajax capabilities.

2 Comments

+1 because this is a valid answer, but you should provide a little more detail.
kind'a like saying "you might want to read .Net Web Services documentation @ msdn.microsoft.com/en-us/library/ms950421.aspx for more information.
0

You need to do something very important: Add the ScriptService attribute the class so that it can be called from a Javascript as in this example:

[ScriptService]
public class SimpleWebService : System.Web.Services.WebService 
{
    // ...
}

http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptserviceattribute.aspx

Comments

0

You have to pass the three parameters from the cs code page, by calling like this way,

service.SendMail(name, email, message);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.