send email C# using smtp server with username password authentification

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);
}
zaranatrivediAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

B HCommented:
where is it getting the username/password for the smtp authentication?

whatever the source is, you need to convert it to base64 and send that instead

http://qmail.jms1.net/test-auth.shtml

or get your base64 here:
http://www.motobit.com/util/base64-decoder-encoder.asp
0
zaranatrivediAuthor Commented:
i have an account 'user@something.com" and i have the password and the outgoing email server port is 25.

i can use the web access and login using that email id and password and send / receive emails

I am sorry i did not understand what do i need to try with base64

any idea .. thank you
0
B HCommented:
here's what an authenticated test looks like - your script should be sending commands like this (with encrypted username and password):
what i say is in bold, what the server says is not:

telnet mail.testing.com 25
220 mail.testing.com Microsoft ESMTP MAIL Service, Version: 6.0.3790.3959 ready at  Wed, 28 Apr 2010 22:23:18 -0400
ehlo testing.com
250-mail.testing.com Hello [my.ip.add.res]
250-TURN
250-SIZE
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-VRFY
250-X-EXPS GSSAPI NTLM LOGIN
250-X-EXPS=LOGIN
250-AUTH GSSAPI NTLM LOGIN
250-AUTH=LOGIN
250-X-LINK2STATE
250-XEXCH50
250 OK
auth login   <-- this says, i'm fixin to authenticate on you
334 VXNlcm5hbWU6    <-- server says, ok go secure, i'll understand you
cmVrandomlettersuMg==   <-- this is my username, encrypted in base64 from that site i posted a link to
334 UGFzc3dvcmQ6     <--- server says, go secure with the password too
T3randomlettersb3Y=  <--  this is my password, encrypted in base64
235 2.7.0 Authentication successful.   <-- yeah i know you, go ahead
mail from: joe@testing.com  <-- standard stuff here, starting to send a mail
250 2.1.0 joe@testing.com....Sender OK    <--- you're allowed to send mail, do it
rcpt to: bryon@testing.com   <-- to this guy
250 2.1.5 bryon@testing.com   <--- yeah you're authenticated so i'll eat it
data  <-- lemmie send you the body
354 Start mail input; end with .    <--- go faster
subject: testing    <-- write the subjectstuff here is in the body   <-- viagra ads, or whatever hereending with enter dot enter  <-- keep talking, time is money.    <-- enter period enter  means i'm done
250 2.6.0  Queued mail for delivery   <-- i will deliver your mail with unholy swiftness

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

B HCommented:
so basically, go to that site, or this one:
http://www.opinionatedgeek.com/dotnet/tools/Base64Encode/

type in your username, convert it to base64, copy the base64 to notepad or something
then reset the form, do the same with your password

then tell your script to feed it as the example above
0
zaranatrivediAuthor Commented:
Oh .. thats new to me

this is what i have

MailMessage msg = new MailMessage(senderEmail, recieverEmail, "afdasfas", "safasfa");
                //MailMessage msg = new MailMessage(senderEmail, recieverEmail, subject, subject);
                SmtpClientEx mailclient = new SmtpClientEx(EmailSmtpServer, outgoingPort);
                System.Net.NetworkCredential auth = new System.Net.NetworkCredential(base64 username,
                    base64Password);
                mailclient.Host = EmailSmtpServer;
                mailclient.UseDefaultCredentials = false;
                mailclient.EnableSsl = false;
                mailclient.Credentials = auth;
                mailclient.Send(msg);
                MessageBox.Show(b.Content + ":WORKED");

senderemail = abc@domain.com
recieverEmail = receiver@gmail.com
EmailSmtpServer = smtp.domain.com

now for username , do i just do base64(username) = base64("abc") = YWJj

and same thing for password .... it didnt work .... the error message is the same (as below )
Do i have to do base64(abc@domain.com)?

{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)
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at TestOutgoingEmailServer.Window1.button3_Click(Object sender, RoutedEventArgs e) in C:\Users\PERTH\Documents\Visual Studio 2008\Projects\TestOutgoingEmailServer\TestOutgoingEmailServer\Window1.xaml.cs:line 185}




 
0
B HCommented:
the username format depends on the server... it could be just abc, or abc@domain.com, or domain\abc

are you sending the command:
auth login
somewhere in there?  their error message "No AUTH command has been given."  leads me to believe you didnt, OR they dont support "auth login", it's some other command

telnet manually to your mail server and issue:
ehlo whatever

it will respond with a bunch of stuff, can you paste that here?  that tells us what kind of authentication it supports

also, it would be a good idea to just manually test the entire session with telnet.

 
0
B HCommented:
also, you could just use the upstream smarthost server for your internet connection (at the web server) too... most internet providers have this, and it usually doesn't require authentication
0
zaranatrivediAuthor Commented:
i tried the command line thing ... my encrypted username and password works fine ... but not able to do that using C# code ...

i used base64 of abc@domain.com as username.


i have the output here

220 smtp.*_SOMEDOMAIN.COM_* -- Server ESMTP (SMTP) Hello Friend!
auth login
334 VXNlcm5hbWU6
Y2*RANDOM-LETTERS*Q==
334 UGFzc3dvcmQ6
*PASSWORD*
535 5.7.8 Bad username or password (Authentication failed).

500 5.5.1 Unknown command "" specified auth login
334 VXNlcm5hbWU6
Y2*_RANDOM-LETTERSmNv_*
334 UGFzc3dvcmQ6
U*_RANDOMLETTERS_*Nh
235 2.7.0 login authentication successful.
421 4.4.2 Timeout while waiting for command.
0
B HCommented:
ok so we know the second encrypted username and encrypted password work - so your script isn't sending "auth login" before the username

i'm requesting attention here because it looks like you tied a real hostname to a valid base64 username and password which is hugely bad, i'll see if they can comment that out for you

note, the bad username is because that password was in plaintext... you need to get your script to send "auth login" as a command before the username

0
zaranatrivediAuthor Commented:
i am using System.Net.Mail api .. and i assume they should have written it that way ... and i need not re-write the SendMail method again in C# ... even if i have to write it ... so you think you can provide an exampl e.... your explanation has been great so far ....
0
B HCommented:
i can't get into the programming aspect very much - but can you cause it to send thru a different server which doesn't require authentication?  most web hosts provide an open relay, restricted to their controlled ip ranges, which would include the machine you're sending from
0
zaranatrivediAuthor Commented:
i still could not get the code to sen email
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Servers

From novice to tech pro — start learning today.