Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello friends,

I am facing a problem in a Web Service. I am giving user the privilege that he can send a mail to a client from the desktop application i.e., our project , on the click of a button. For that I have made a web service but on sending a mail through that an error is coming that: "The request failed with HTTP status 401: Unauthorized".

I have added this web service to my web server and have added a web reference for performing the task through my desktop application after passing appropriate parameters to the send function.

I couldn't able to found an appropriate solution to this problem on any blog so i decided at last to give Code Project a try. I am attaching the code which is being executed on the web service.


public string sendEmail(string to_mail, string from_mail, string cc_mail, string bcc_mail, string subject, string body)<br />
    {<br />
        try<br />
        {<br />
            MailMessage mailuser = new MailMessage(from_mail, to_mail, subject, body);<br />
            if (bcc_mail != "")<br />
            {<br />
                MailAddress bccCopy = new MailAddress(bcc_mail);<br />
                mailuser.Bcc.Add(bccCopy);<br />
            }            <br />
            if (cc_mail != "")<br />
            {<br />
                MailAddress ccCopy = new MailAddress(cc_mail);<br />
                mailuser.CC.Add(ccCopy);<br />
            }            <br />
            //mailuser.Attachments.Add(fileName);<br />
            //Attachment Attachment = new Attachment(file);<br />
            //mailuser.Attachments.Add(Attachment);<br />
<br />
            mailuser.IsBodyHtml = true;            <br />
<br />
            NetworkCredential basicAuthenticationInfo = new NetworkCredential("<user name="">", "<password>");<br />
            SmtpClient mailClient = new SmtpClient();<br />
            mailClient.Host = "<host address="">";<br />
            mailClient.UseDefaultCredentials = false;<br />
            mailClient.Credentials = basicAuthenticationInfo;<br />
<br />
            mailClient.Send(mailuser);<br />
            return "Mail Sent Successfully";<br />
        }<br />
        catch(Exception ex)<br />
        {<br />
            return ex.Message;<br />
        }<br />
    }<br />
</host></password></user>

If any one out there can help me resolve this error, I would appreciate his/her concern.

Thanks
Posted

try
{
string to = "help@xxx.com";
string from = "help@xxx.com";
string subject = "Help";
string body = "First name:- " + txtFirstName.Text + Environment.NewLine + "Last Name :-" + txtLastName.Text + Environment.NewLine + "Email:- " + txtEmail.Text + Environment.NewLine + "Ph No:- " + txtMobileNo.Text + Environment.NewLine + "Messsage:-" + txtPleasefeel.Text.Trim();
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.gmail.com",587);

client.EnableSsl = true;// enable must in webconfig file

client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("help@xxx.com","password"); // password-as ur
client.Send(message);
litStatus.Text = "Sent Successfully...";
txtFirstName.Text = "";
txtMiddleName.Text = "";
txtLastName.Text = "";
txtEmail.Text = "";
txtMobileNo.Text = "";
txtPleasefeel.Text = "";
}
catch (Exception ex)
{
litStatus.Visible = true;
litStatus.Text = "Server OverLoaded...";
}


///////////////////////////////////////////
in web config file at last

XML
<system.diagnostics>
   <trace autoflush="true" />
   <sources>
     <source name="System.Net" >
       <listeners>
         <add name="MyTraceFile"/>
       </listeners>
     </source>
     <source name="System.Net.Sockets">
       <listeners>
         <add name="MyTraceFile"/>
       </listeners>
     </source>
   </sources>

   <sharedListeners>
     <add
       name="MyTraceFile"
       type="System.Diagnostics.TextWriterTraceListener"
       initializeData="System.Net.trace.log" />
   </sharedListeners>
   <switches>
     <add name="System.Net" value="Verbose" />
     <add name="System.Net.Sockets" value="Verbose" />
   </switches>
 </system.diagnostics>
 
Share this answer
 
Comments
Varun Sareen 17-Aug-10 12:47pm    
Reason for my vote of 3
Gud one dear suzeets...thanks but i found the solution myself only.there was some anonymous checkbox unchecked in IIS virtual directory..
Your web server doesn't allow the user to access the service. I guess you service is not even called, because the web server blocks out the user.

Please make sure that there is no HTTP protection on any directories in the web service's path.
Check your proxy settings and make sure they are actually used.
 
Share this answer
 
Comments
Varun Sareen 17-Aug-10 12:49pm    
Reason for my vote of 3
I got the solution myself dear Corinna..some anonymous check box was unchecked.in IIS virtual directory..so the request was not accepted :)

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