SMTPClient + NetworkCredential .NET v2.0

Nate Bross

Contributor
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
I'm using .NET 2.0 trying to send email.

The issue is that in the old days with CDOSYS I could do

C#:
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");

which would Authenitcate using "login" or "1"

I cannot figure out how to get this functionallity in .NET 2.0.

I can successfully use the NetworkCredentail to send to other mail servers.

Thanks in advance.
 
Last edited by a moderator:
If you use System.Net.Mail and want to authenticate yourself with AUTH-LOGIN, MD5 or NTLM you CANNOT!
System.Net.Mail does not support authentication.
 
Thanks Much for the response, is there a work around? How can this functionallity be achieved? or is this type of authentication depreciated and not widely used?
 
who told you that you can NOT Authenticate with System.Net.Mail?
i put some simple code together & logged on to gmail & sent an email to another address, all you need to do is set your NetworkCredentials up.
some email servers such as yahoo don't seem to work anymore with this method, but the same applies to the old mailclient ( System.Web.Mail )
this is a basic example that may point you in the right direction...
Code:
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mAddress [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MailAddress(txtUser.Text, txtUser.Text)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mMessage [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MailMessage[/SIZE]
[SIZE=2][COLOR=#008000]'[/COLOR][/SIZE]
[SIZE=2]mMessage.To.Add(txtTo.Text)[/SIZE]
[SIZE=2]mMessage.To.Add(txtUser.Text) [COLOR=seagreen]'/// added my own address to the To field[/COLOR][/SIZE]
[SIZE=2]mMessage.From = mAddress[/SIZE]
[SIZE=2]mMessage.Sender = mAddress[/SIZE]
[SIZE=2]mMessage.Subject = txtSubject.Text[/SIZE]
[SIZE=2]mMessage.IsBodyHtml = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess [/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] DeliveryNotificationOptions.OnFailure[/SIZE]
[SIZE=2]mMessage.Priority = MailPriority.High[/SIZE]
[SIZE=2]mMessage.Body = rtfBody.Text[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] client [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SmtpClient([/SIZE][SIZE=2][COLOR=#a31515]"smtp.googlemail.com"[/COLOR][/SIZE][SIZE=2], 587)[/SIZE]
[SIZE=2]client.EnableSsl = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]client.UseDefaultCredentials = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]client.Credentials = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] NetworkCredential(txtUser.Text, txtPass.Text)[/SIZE]
[SIZE=2]client.Send(mMessage)[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Mail.SmtpException[/SIZE]
[SIZE=2]Console.WriteLine(ex.Message)[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
 
Yes, I can use the NetworkCredentail to authenticate to some email servers; however, the NetworkCredential does not allow me to send mail through servers that require "login" type set to "1" as in the old says with CDOSYS (or CDONTS before that).
 
This issue has raised it's head again for me and I never found a solution.

I have this code behind two buttons on a sample Windows Forms Application.


This Code does not work.
C#:
        private void button1_Click(object sender, EventArgs e)
        {
            System.Net.Mail.MailAddress maFrom = new System.Net.Mail.MailAddress("nate.b@company.com"); 
            System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
            mm.From = maFrom;
            
            mm.To.Add("nate.b@company.com");
            
            mm.Subject = "System.Net.Mail";
            mm.Body = "System.Net.Mail";

            System.Net.Mail.SmtpClient sn = new System.Net.Mail.SmtpClient("MAILSERVER", 25);
            sn.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            sn.Credentials = new System.Net.NetworkCredential("MailUser", "MailPass"); 
            sn.UseDefaultCredentials = false;
            
            sn.Send(mm);

            MessageBox.Show("OK");
        }

Returning this error message:

Code:
Transaction failed. The server response was: 5.7.1 <nate.b@company.com>: Relay access denied

While this code works just fine.
C#:
        private void button2_Click(object sender, EventArgs e)
        {
            System.Web.Mail.MailMessage mm = new System.Web.Mail.MailMessage();
            //mm.ReplyTo = maFrom;
            mm.From = "nate.b@company.com";

            mm.To = "nate.b@company.com";

            mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "MailUser");
            mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "MailPass");


            mm.Subject = "System.Web.Mail";
            mm.Body = "System.Web.Mail";

            System.Web.Mail.SmtpMail.SmtpServer = "MAILSERVER";
            System.Web.Mail.SmtpMail.Send(mm);

            MessageBox.Show("OK");
        }

I've isolated the only variable to be the use of different namespaces (and classes therein).

Same email accounts, same smtp server.

My Question is "how can I emulate these lines [below] using the System.Net.Mail namespace?"

The most important line is the first since the other two lines are generally done via System.Net.NetworkCredentail object.

C#:
mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
C#:
mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "MailUser");
mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "MailPass");

Thanks in Advanced!
 
I read somewhere to try System.Net.Tracing in my application configuration file, I've done that; however, I'm not sure how to read the results. Any help is really appreciated.

Code:
System.Net Information: 0 : [6020] Associating MailMessage#2637164 with Message#41014879
System.Net Verbose: 0 : [6020] SmtpClient::.ctor(host=osaka, port=25)
System.Net Information: 0 : [6020] Associating SmtpClient#3888474 with SmtpTransport#25209742
System.Net Verbose: 0 : [6020] Exiting SmtpClient::.ctor() 	-> SmtpClient#3888474
System.Net Verbose: 0 : [6020] SmtpClient#3888474::Send(MailMessage#2637164)
System.Net Information: 0 : [6020] SmtpClient#3888474::Send(DeliveryMethod=Network)
System.Net Information: 0 : [6020] Associating SmtpClient#3888474 with MailMessage#2637164
System.Net Information: 0 : [6020] Associating SmtpTransport#25209742 with SmtpConnection#26966483
System.Net Information: 0 : [6020] Associating SmtpConnection#26966483 with ServicePoint#31884011
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Socket() 
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Connect(144:25#-1878390253)
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Connect() 
System.Net Information: 0 : [6020] Associating SmtpConnection#26966483 with SmtpPooledStream#44150175
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Receive()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Receive
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 32 32 30 20 66 77 69 7A-2E 63 6F 6D 20 45 53 4D : 220 company.com ESM
System.Net.Sockets Verbose: 0 : [6020] 00000010 : 54 50 20 50 6F 73 74 66-69 78 0D 0A             : TP Postfix..
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Receive() 	-> 28#28
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Send()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Send
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 45 48 4C 4F 20 4E 61 74-65 42 6F 6F 6B 0D 0A    : EHLO NateBook..
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Send() 	-> 15#15
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Receive()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Receive
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 32 35 30 2D 66 77 69 7A-2E 63 6F 6D 0D 0A 32 35 : 250-company.com..25
System.Net.Sockets Verbose: 0 : [6020] 00000010 : 30 2D 50 49 50 45 4C 49-4E 49 4E 47 0D 0A 32 35 : 0-PIPELINING..25
System.Net.Sockets Verbose: 0 : [6020] 00000020 : 30 2D 53 49 5A 45 0D 0A-32 35 30 2D 56 52 46 59 : 0-SIZE..250-VRFY
System.Net.Sockets Verbose: 0 : [6020] 00000030 : 0D 0A 32 35 30 2D 41 55-54 48 20 4C 4F 47 49 4E : ..250-AUTH LOGIN
System.Net.Sockets Verbose: 0 : [6020] 00000040 : 20 50 4C 41 49 4E 20 43-52 41 4D 2D 4D 44 35 20 :  PLAIN CRAM-MD5 
System.Net.Sockets Verbose: 0 : [6020] 00000050 : 47 53 53 41 50 49 0D 0A-32 35 30 2D 45 54 52 4E : GSSAPI..250-ETRN
System.Net.Sockets Verbose: 0 : [6020] 00000060 : 0D 0A 32 35 30 2D 45 4E-48 41 4E 43 45 44 53 54 : ..250-ENHANCEDST
System.Net.Sockets Verbose: 0 : [6020] 00000070 : 41 54 55 53 43 4F 44 45-53 0D 0A 32 35 30 2D 38 : ATUSCODES..250-8
System.Net.Sockets Verbose: 0 : [6020] 00000080 : 42 49 54 4D 49 4D 45 0D-0A 32 35 30 20 44 53 4E : BITMIME..250 DSN
System.Net.Sockets Verbose: 0 : [6020] 00000090 : 0D 0A                                           : ..
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Receive() 	-> 146#146
System.Net Verbose: 0 : [6020] SmtpNegotiateAuthenticationModule#65326725::Authenticate()
System.Net Information: 0 : [6020] Enumerating security packages:
System.Net Information: 0 : [6020]     Negotiate
System.Net Information: 0 : [6020]     Kerberos
System.Net Information: 0 : [6020]     NTLM
System.Net Information: 0 : [6020]     Microsoft Unified Security Protocol Provider
System.Net Information: 0 : [6020]     Schannel
System.Net Information: 0 : [6020]     WDigest
System.Net Information: 0 : [6020]     DPA
System.Net Information: 0 : [6020]     Digest
System.Net Information: 0 : [6020]     MSN
System.Net Information: 0 : [6020] AcquireCredentialsHandle(package  = Negotiate, intent   = Outbound, authdata = testuser)
System.Net Information: 0 : [6020] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = , inFlags = Connection, InitIntegrity)
System.Net Information: 0 : [6020] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=40, returned code=ContinueNeeded).
System.Net Verbose: 0 : [6020] Exiting SmtpNegotiateAuthenticationModule#65326725::Authenticate() 
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Send()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Send
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 41 55 54 48 20 67 73 73-61 70 69 20 54 6C 52 4D : AUTH gssapi TlRM
System.Net.Sockets Verbose: 0 : [6020] 00000010 : 54 56 4E 54 55 41 41 42-41 41 41 41 6C 34 49 49 : TVNTUAABAAAAl4II
System.Net.Sockets Verbose: 0 : [6020] 00000020 : 34 67 41 41 41 41 41 41-41 41 41 41 41 41 41 41 : 4gAAAAAAAAAAAAAA
System.Net.Sockets Verbose: 0 : [6020] 00000030 : 41 41 41 41 41 41 41 46-41 53 67 4B 41 41 41 41 : AAAAAAAFASgKAAAA
System.Net.Sockets Verbose: 0 : [6020] 00000040 : 44 77 3D 3D 0D 0A                               : Dw==..
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Send() 	-> 70#70
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Receive()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Receive
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 35 33 35 20 35 2E 37 2E-30 20 45 72 72 6F 72 3A : 535 5.7.0 Error:
System.Net.Sockets Verbose: 0 : [6020] 00000010 : 20 61 75 74 68 65 6E 74-69 63 61 74 69 6F 6E 20 :  authentication 
System.Net.Sockets Verbose: 0 : [6020] 00000020 : 66 61 69 6C 65 64 3A 20-67 65 6E 65 72 69 63 20 : failed: generic 
System.Net.Sockets Verbose: 0 : [6020] 00000030 : 66 61 69 6C 75 72 65 0D-0A                      : failure..
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Receive() 	-> 57#57
System.Net Verbose: 0 : [6020] SmtpLoginAuthenticationModule#61150033::Authenticate()
System.Net Verbose: 0 : [6020] Exiting SmtpLoginAuthenticationModule#61150033::Authenticate() 
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Send()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Send
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 41 55 54 48 20 6C 6F 67-69 6E 20 56 32 6C 36 51 : AUTH login V2l6Q
System.Net.Sockets Verbose: 0 : [6020] 00000010 : 55 52 63 64 47 56 7A 64-48 56 7A 5A 58 49 3D 0D : URcdGVzdHVzZXI=.
System.Net.Sockets Verbose: 0 : [6020] 00000020 : 0A                                              : .
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Send() 	-> 33#33
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Receive()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Receive
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 33 33 34 20 56 58 4E 6C-63 6D 35 68 62 57 55 36 : 334 VXNlcm5hbWU6
System.Net.Sockets Verbose: 0 : [6020] 00000010 : 0D 0A                                           : ..
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Receive() 	-> 18#18
System.Net Verbose: 0 : [6020] SmtpLoginAuthenticationModule#61150033::Authenticate()
System.Net Verbose: 0 : [6020] Exiting SmtpLoginAuthenticationModule#61150033::Authenticate() 
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Send()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Send
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 56 47 56 7A 64 46 42 68-4E 54 55 3D 0D 0A       : VGVzdFBhNTU=..
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Send() 	-> 14#14
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Receive()
System.Net.Sockets Verbose: 0 : [6020] Data from Socket#32176063::Receive
System.Net.Sockets Verbose: 0 : [6020] 00000000 : 33 33 34 20 55 47 46 7A-63 33 64 76 63 6D 51 36 : 334 UGFzc3dvcmQ6
System.Net.Sockets Verbose: 0 : [6020] 00000010 : 0D 0A                                           : ..
System.Net.Sockets Verbose: 0 : [6020] Exiting Socket#32176063::Receive() 	-> 18#18
System.Net Verbose: 0 : [6020] SmtpLoginAuthenticationModule#61150033::Authenticate()
System.Net Verbose: 0 : [6020] Exiting SmtpLoginAuthenticationModule#61150033::Authenticate() 
System.Net Error: 0 : [6020] Exception in the SmtpClient#3888474::Send - Authentication failed.
System.Net Error: 0 : [6020]    at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
System.Net.Sockets Verbose: 0 : [6020] Socket#32176063::Dispose()
System.Net Error: 0 : [6020] Exception in the 
#12549444::UnhandledExceptionHandler - Authentication failed.

Thanks in advance!
 
Hello Nate Bross,

Have you found the solution of this problem?

I have two vesrion of email sender application:
1. Based on System.Web.Mail (works fine)
2. Based on System.Net.Mail (Cant authenticate)

Is there any ideas how to make it works on System.Net.Mail??
 
I ended up setting up IIS Smtp Relay service inbetween.

So now my application talks to IIS, which in turn talks to the mail server. Its not pretty, but it works for me.
 
Back
Top