c# - Authenticating with a non-compliant SMTP server -
i'm supposed send e-mail through smtp server. smtp server requires authentication. however, if use default authentication mechanism of .net smtpclient, authentication fails 504 unrecognized authentication type
.
when using putty i'm able authenticate:
250 auth login plain auth login 334 vxnlcm5hbwu6 base64 encoded username 334 ugfzc3dvcmq6 base64 encoded password 235 authentication successful
the smtpclient
using different approach, username passed in auth login command:
250 auth login plain auth login base64 encoded username 504 unrecognized authentication mail from: ...
it seems ignoring 504, because sending mail command. reading sources on internet, allowed send username auth login command:
however, there exists different, rfc compliant version of behavior, client sends userid auth login method
the problem @ smtp server's side, have no control on it.
is possible make smtpclient send commands separately?
sample code
using (var client = new smtpclient(_configuration.host, _configuration.port)) { client.usedefaultcredentials = false; client.servicepoint.maxidletime = 1; client.enablessl = false; client.credentials = new networkcredential(_configuration.username, _configuration.password, _configuration.domain); var = new mailaddress(string.format("{0}@{1}", _configuration.sender, _configuration.domain)); var = new mailaddress(string.format("{0}@{1}", to, _configuration.domain)); var message = new mailmessage(from, to) { body = body }; client.send(message); }
diy
just fun (and not production) i've tried own communication:
using (var connection = new tcpclient(_configuration.host, _configuration.port)) using (networkstream stream = connection.getstream()) { string header = read(stream); reply(stream, "ehlo " + environment.machinename); while (!read(stream).toupperinvariant().contains("auth login plain")) { } reply(stream, "auth login"); string requestusername = read(stream); reply(stream, convert.tobase64string(encoding.ascii.getbytes(_configuration.username))); string requestpassword = read(stream); reply(stream, convert.tobase64string(encoding.ascii.getbytes(_configuration.password))); string authsucces = read(stream); reply(stream, string.format("mail from: <{0}@{1}>", _configuration.sender, _configuration.domain)); string senderok = read(stream); reply(stream, string.format("rcpt to: <{0}@{1}>", to, _configuration.domain)); string rcptok = read(stream); reply(stream, "data"); string senddata = read(stream); reply(stream, body); reply(stream, string.empty); reply(stream, "."); reply(stream, string.empty); stream.close(); connection.close(); } private string read(networkstream stream) { var data = new byte[1024]; int received = stream.read(data, 0, data.length); string contents = encoding.ascii.getstring(data, 0, received); return contents; } private void reply(networkstream stream, string reply) { reply = reply + environment.newline; stream.write(encoding.ascii.getbytes(reply), 0, reply.length); stream.flush(); }
this works great. how smtpclient
, actual smtp server communicate.
work-around
it turns out have been communicating system implemented smtp protocol , did wrong. have decided communication ourselves, until vendor has fixed code. luckily internal purposes , communication predictable. using diy-code refactoring , cleanup.
i had same problem here solution/workaround: sending email using system.web.mail instead of system.net.mail. know not best possible solution because library deprecated may until vendor fixes code.
system.web.mail.mailmessage msg = new system.web.mail.mailmessage(); msg.body = message.body; string smtpserver = "mail.business.it"; string username = "username"; string password = "password"; int cdobasic = 1; int cdosendusingport = 2; if (username.length > 0) { msg.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpserver); msg.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25); msg.fields.add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdosendusingport); msg.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdobasic); msg.fields.add("http://schemas.microsoft.com/cdo/configuration/sendusername", username); msg.fields.add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); } msg.to = message.destination; msg.from = "me@domain.it"; msg.subject = message.subject; msg.bodyformat = mailformat.html;//system.text.encoding.utf8; smtpmail.smtpserver = smtpserver; smtpmail.send(msg);
Comments
Post a Comment