Home > ASP.Net > Send Email using WebService

Send Email using WebService


Hi

try this example to Send Email using Web Service

You can check this post Send Email using Gmail Account

1) Open VS2008 and Create a new Website

2)  Add new Web Page and add Three Html input controls of Type Text and ScriptManager Control from ToolBox

<div>
    <asp:ScriptManager ID="ScriptManager" runat="server"
       EnablePageMethods="true" />
   <fieldset id="ContactFieldset">
       <label>
           Your Name
           <input type="text" id="NameTextBox" /></label>
       <label>
           Email Address
           <input type="text" id="EmailTextBox" /></label>
       <label>
           Your Message
           <textarea id="MessageTextBox"></textarea></label>
       <button onclick="SendForm();">
           Send</button>
   </fieldset>
    </div>

3) Add this Javascript code :-


    <script type="text/javascript">
        function SendForm() {
            var name = $get("NameTextBox").value;
            var email = $get("EmailTextBox").value;
            var message = $get("MessageTextBox").value;

            PageMethods.SendForm(name, email, message,OnSucceeded, OnFailed);
        }

        function OnSucceeded() {
            // Dispaly "thank you."
            $get("ContactFieldset").innerHTML = "<p>Thank you!</p>";
        }

        function OnFailed(error) {
            // Alert user to the error.
            alert(error.get_message());
        }
    </script>

4) In code behind add the following code :-


[WebMethod]
    public static void SendForm(string name, string email, string message)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new Exception("You must supply a name.");
        }

        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        if (!regex.IsMatch(email))
        {
            throw new Exception("You must Enter Valid Email.");
        }

        if (string.IsNullOrEmpty(email))
        {
            throw new Exception("You must supply an email address.");
        }

        if (string.IsNullOrEmpty(message))
        {
            throw new Exception("Please provide a message to send.");
        }

        // If we get this far we know that they entered enough data, so
        // here is where you would send the email or whatever you wanted
        // to do 🙂
        try
        {
            string mailServerName = "smtp.gmail.com";
            MailMessage messages = new MailMessage(email, <a href="mailto:yourmail@gmail.com">yourmail@gmail.com</a>, "Test", message);
            messages.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = mailServerName;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = <a href="mailto:yourmail@gmail.com">yourmail@gmail.com</a>;
            NetworkCred.Password = "password";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(messages);
        }
        catch
        {
            throw new Exception("Fail in Sending Email.");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

Don’t Forget to use this namespace

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

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. July 6, 2014 at 2:28 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: