Click here to Skip to main content
15,889,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have created a web service for sending mails and while everything seems to be ok and the .asmx is working, when Im trying to use it through my the .aspx page it is not working.Could you take a look please.

Web Service
C#
public class mail : System.Web.Services.WebService {

    public mail () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public void SendMail(string mail,string subject, string body)
    {
        // Gmail Address from where you send the mail
        var fromAddress = "xnethardware@gmail.com";
        // any address where the email will be sending
        var toAddress = mail.ToString();
        //Password of your gmail address
        const string fromPassword = "xnet1234";
     
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, mail, subject, body);
    }
   
}


aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

public partial class contact : System.Web.UI.Page
{



    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //here on button click what will done 
            localhost.mail minima = new localhost.mail();
            minima.SendMail(mail.Text, subject.Text, body.Text);
            DisplayMessage.Text = "Your Comments after sending the mail";
            DisplayMessage.Visible = true;
           
        }
        catch (Exception) { }
        
    }
}


Would appreciate any help since im new to coding.
Posted
Updated 6-Jul-13 5:42am
v2
Comments
[no name] 6-Jul-13 11:29am    
It's really helpful to tell us what "it is not working" means. Did you debug it? Do you get any errors? My guess that you are probably getting an exception of some kind but your empty catch is just throwing it away.
Member 10143124 6-Jul-13 12:55pm    
Well it simply does nothing.Im pressing the button and nothing happens.When im trying to run the WS by itself though its working flawlessly.I dont get any errors during debug
[no name] 6-Jul-13 16:29pm    
"does nothing" is equally as meaningless as "it is not working". You are doing the equivalent of calling your mechanic on the phone and telling him that your car "is broke" and expecting that he can tell you what is wrong. You are not getting any errors when you debug it because you are simply throwing the errors away.

bool SendMail (string strFrom, string strPass, string strTo, string strSub, string strBody)
	{
            SmtpClient smtpClient = new SmtpClient( "smtp.gmail.com", 465 );
            System.Net.NetworkCredential credentials = null ;
             bool bSuccess = true ;
            
            MailMessage message = new MailMessage();
            // Try to send the message
            try
            {
                // Prepare two email addresses 
                MailAddress fromAddress = new MailAddress(strFrom);
                MailAddress toAddress = new MailAddress (strTo);
                // Prepare the mail message
                message.From = fromAddress;
                message.To.Add(toAddress);
                message.Subject = strSub;
                message.IsBodyHtml = true;
                credentials = new System.Net.NetworkCredential( strFrom, strPass);
                smtpClient.Credentials = credentials;
                smtpClient.Send(message); 
            }
            catch ( Exception eX )
            {
                 // eX.Message shows  the cause of the error, if it failes.
                 bSuccess = false ;
            }
            return bSuccess ;
     }
 
Share this answer
 
v2
Comments
_Asif_ 6-Jul-13 13:35pm    
We would like to know how you add web service reference in your project because it seems that web service is not getting fired. you can directly check it by placing break point on the button click event and then debug further.
Member 10143124 6-Jul-13 13:45pm    
Im not sure I understand exactly what you are asking.I have created the web service (add new > web service) and once I coded it I right clicked on my solution and then add web reference.
I have 4 more WS running at the same project which are fired up by button clicks but this is the only one that is not working.

Appreciate the help
_Asif_ 6-Jul-13 13:57pm    
Switch debugging to multiple projects. Set two starting points. One is your web service project that is creating problem. Second one is your web application that is consuming your web service. Put break points on sendmail web method and on your button click event and start debugging and see what is happening
Member 10143124 6-Jul-13 14:33pm    
System.Net.WebException was unhandled by user code
HResult=-2146233079
Message=The request failed with HTTP status 404: Not Found.
Source=System.Web.Services
StackTrace:
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at localhost.mail.SendMail(String to, String subj, String body) in c:\Users\Passas\AppData\Local\Temp\Temporary ASP.NET Files\x-netfinal\3ff488d8\b17cbc5\App_WebReferences.2vp9ghas.0.cs:line 214
at contact.Button1_Click(Object sender, EventArgs e) in c:\Users\Passas\Documents\Visual Studio 2010\WebSites\X-NetFinal\contact.aspx.cs:line 17
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

This is what im getting.The error appears at the aspx.cs page right after SendMail() is called
The request failed with HTTP status 404:Not found
_Asif_ 6-Jul-13 14:45pm    
web application project unable to find webservice url. Check the location, port of the hosted web service. Should be like this http:///localhost:24356/yourwebserviceproject. See if this is still valid.

One additional thing you can check is deploy your web service separately on IIS and link this webservice to your web project.
I updated all the web services in my project and re added web references and everything worked
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900