views:

847

answers:

2

I have a piece of code that sends email.. heres the code

This is not working for me. This a remote smtp service ... and i double checked that email web access works fine .. i can login using the gui, recieve and send emails.

But when i try to do it through code .. it fails with the message ...

{System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 No AUTH command has been given.

Can anybody advise ... and also they dont have EWS exposed ie.e exchange web service ./.. this is the way to go ..

port is 25 and no SSL or TLS

Button b = sender as Button;
try
{
    MailMessage msg = new MailMessage(senderEmail, recieverEmail, "afdasfas", "safasfa");
    //MailMessage msg = new MailMessage(senderEmail, recieverEmail, subject, subject);
    System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient(EmailSmtpServer, outgoingPort);
    System.Net.NetworkCredential auth = new System.Net.NetworkCredential(senderEmail, senderPassword);
    mailclient.Host = EmailSmtpServer;
    mailclient.UseDefaultCredentials = false;
    mailclient.Credentials = auth;
    mailclient.Send(msg);
    MessageBox.Show(b.Content + ":WORKED");
}
catch (Exception e4)
{
    MessageBox.Show(b.Content + ": " +e4.Message);
    MessageBox.Show(b.Content + ": " + e4.StackTrace);
}
+2  A: 

I don't think you want to set

mailclient.UseDefaultCredentials = true;

Read more about this option here.


You should also test that you can actually send email through that smtp server.

Try connecting via telnet and authorizing. There are also plenty of services that will let you send a test message online (http://pingability.com/smtptest.jsp for example).

Joel Potter
No .. i am sorry thas a typo .. i was trying all options ..i will edit the post ... i get this exception when i trying run the code{System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 No AUTH command has been given. at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
pskk
@KK, see my edit.
Joel Potter
i can telnet to that remote smtp service ... when i use auth login and then provide username and password in base64 format , i can login ... so i guess the problem in with C# code ... and sendMail does not send "auth login" command or does not send username/password in base64 ... do you think you can point me to blog/site which has this corrected ... i tried a bunch of them and it didnt work for me ... thank you.
pskk
@KK, The code you have looks good. Are you passing the username and password to the NetworkCredential constructor in plain text?
Joel Potter
Also, check your firewall.
Joel Potter
i pass it as base64 .. i can write a C# script and do the selnet thing .. and send email .. everything works fine in Tx, USA ..state where i am located ... even from my home network .... but does not work when i run it from my client in columbia ...
pskk
A: 

maybe you need to Add mailclient.EnableSsl = true;

Yassir
No .. i am sorry thas a typo .. i was trying all options ..i will edit the post ... i get this exception when i trying run the code{System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 No AUTH command has been given. at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
pskk
the remote smtp service does not need smtp enabled
pskk
i can telnet to that remote smtp service ... when i use auth login and then provide username and password in base64 format , i can login ... so i guess the problem in with C# code ... and sendMail does not send "auth login" command or does not send username/password in base64 ... do you think you can point me to blog/site which has this corrected ... i tried a bunch of them and it didnt work for me ... thank you.
pskk