C # SMTP error but credentials are correct

Here is my problem: I wrote the following program to check if I can send an email:

class Program { static void Main(string[] args) { try { Console.WriteLine("Mail To"); MailAddress to = new MailAddress(" myemail@gmail.com "); Console.WriteLine("Mail From"); MailAddress from = new MailAddress(" me@businessdomain.it "); MailMessage mail = new MailMessage(from, to); Console.WriteLine("Subject"); mail.Subject = "test"; Console.WriteLine("Your Message"); mail.Body = "test"; SmtpClient smtp = new SmtpClient(); smtp.Host = "mail.domain.it"; smtp.Port = 25; smtp.Credentials = new NetworkCredential( "username", "password"); smtp.EnableSsl = false; Console.WriteLine("Sending email..."); smtp.Send(mail); }catch(Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } } } 

The credentials are correct (I tested them using telnet, Outlook and the k9mail application in android to work correctly), the program works if I install gmail smtp. I really can not understand the cause of this error.

Using wirehark, I discovered what was going on: S: 220 server1.business.it SMTP server ready
C: EHLO pc14
S: 250 server1.stargatenet.it Hello [87.28.219.65] | 250 PIPELINE | 250 SIZE 25000000 | 250 8BITMIME | 250 BINARYMIME | 250 CHUNKING | 250 AUTH LOGIN CRAM-MD5 DIGEST-MD5 | 250 Ok
C: AUTH login User: UsernameBase64
S: 334 VXNlcm5hbWU6
C: Pass: PasswordBase64
S: 334 UGFzc3dvcmQ6

The program does not seem to enter credentials when asked. How is this possible?

0
source share
1 answer

This link saved my life: link

here the problem that I experienced is well described: even if the username is transmitted with AUTH LOGIN, the server answers AUTH_LOGIN_Username_Challenge again.

This problem only occurs when using System.Net.Mail. The link offers the following possible solutions:

1) Use CDOSYS (does not send username with AUTH Login)
2) Use System.Web.Mail (Do not send username with AUTH Login)
3) Contact the owner of the SMTP server and fix the server.

Unfortunately, I could not use the owner of the SMTP server, so I had to use System.Web.Mail. I know that it is outdated, but, unfortunately, in such cases there is no other choice for IMO.

What is my working 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); 

This answer helped me in using System.Web.Mail.

+1
source

Source: https://habr.com/ru/post/1234281/


All Articles