I want to send a mail message with the SmtpClient class.
Here’s the code I use:
SmtpClient smtpClient = new SmtpClient("Host",25);
NetworkCredential basicCredential =
new NetworkCredential("UserName", "Password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("me@domain.com");
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "test send";
message.IsBodyHtml = true;
message.Body = "<h1>hello</h1>";
message.To.Add("mail@domain.com");
smtpClient.Send(message);
But it always throws an exception:
The server committed a protocol violation The server response was: UGFzc3dvcmQ6
I can’t find the reason for that. Please, if anyone has faced something like this, tell me what to do.
This looks to me like SmtpClient authentication is somehow getting out of step.
Some authentication mechanisms are “Client: request auth with username and password, Server: success/fail” others are “Client: request auth with username, Server: request password, Client: reply with password, Server: success/fail”.
It looks like SmtpClient is expecting the former, while your server is expecting the latter.
As dave wenta suggested, a log of a session would tell you what auth mechanism SmtpClient is trying to use, but it will also say what auth mechanisms the server supports.
What normally happens is that the server offers a number of authetication options, and the client choses which one it is going to use. The behaviour from there should be determined by the protocol chosen. I would hope that the SmtpClient class took care of that for you though, but I’m afraid I’ve never used that particular class.
Also remember – If you are going to post a log here, change to a throwaway password before you log the session, as a base64 encoded plain text password can be trivially changed back to human readable plain text password.