0

Here is my problem: I wrote whe following program to test if I can send 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 app k9mail in android correctly work), the program works if I put gmail smtp setting. I really cannot understand the cause of this error.

Using wireshark I have discovered what is happening: S: 220 server1.business.it SMTP Server ready
C: EHLO pc14
S: 250 server1.stargatenet.it Hello [87.28.219.65] | 250 PIPELINING | 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

It seems like the program is not entering the credentials when asked. How is that possible?

silviagreen
  • 1,679
  • 1
  • 18
  • 39

3 Answers3

1

This link saved my life: link

here the problem I experienced is well described: even though the user name is passed with the AUTH LOGIN the server responds with the AUTH_LOGIN_Username_Challenge again.

This problem happens only using System.Net.Mail. The link suggests tre possible solutions:

1)Use CDOSYS (Does not send the User Name with the AUTH Login)
2)Use System.Web.Mail (Does not send the User Name with the AUTH Login)
3)Contact the SMTP Server owner and have them fix the server.

Unfortunately I couldn't the SMTP server owner, so I had to use System.Web.Mail. I know it is deprecated but unfortunately in cases like this there is no other choice IMO.

That's 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.

Community
  • 1
  • 1
silviagreen
  • 1,679
  • 1
  • 18
  • 39
0

While I had encountered the same issue, I found a solution to the issue here

In short, you must use

smtp.UseDefaultCredentials = false;

AFTER

smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email.username"].ToString(), 
                                         ConfigurationManager.AppSettings["email.password"].ToString());

So quick summary, something that would look more like this should work:

        SmtpClient smtp = new SmtpClient();
        smtp.Host = ConfigurationManager.AppSettings["email.smtp"].ToString();
        smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["email.port"]);
        smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email.username"].ToString(), 
                                                 ConfigurationManager.AppSettings["email.password"].ToString());
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["email.enablessl"]);
        Console.WriteLine("Sending email..." + (i+1));
        smtp.Send(mail);
Mike
  • 29
  • 6
  • I'm pretty sure this is wrong. `UseDefaultCredentials` and `Credentials` use the same underlying value, which is set to `null` if you set `UseDefaultCredentials` to `false`. You are effectively erasing your credentials immediately after setting them. If you set `Credentials` you don't need to set `UseDefaultCredentials`. – Dave Cousineau Mar 03 '22 at 00:15
0

For anyone interested. I had a problem t=with this today. and found my solution was the order.

SMTPClient client = new SMTPClient();
client.UseDefaultCredentials = false; // first after declare and needs to be false.

Then:

client.Credentials = new System.Net.NetworkCredential(sMTPSettings.SMTPLogin, sMTPSettings.SMTPPassword);

Then the rest.

Hope it helps someone

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
John Irvine
  • 156
  • 2
  • 7
  • they use the same underlying value; there is no reason to set `UseDefaultCredentials` if you are already setting `Credentials`. – Dave Cousineau Mar 03 '22 at 00:21