C# Sending an email using default credentials: exception












0















I am trying to send an email using default credentials and the SMTPClient.



The exception I get is:




The SMTP Server requires a secured connection, or the client was not authenticated. The serverresponse was: 5.7.1 Client was not authenticated.




The code I am trying to use:



public void SendEmail(List<string> recipients, string subject, string body)
{
if (recipients.Count == 0)
return;

MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient();
foreach (string to in recipients)
{
mail.To.Add(to);
}

mail.From = new MailAddress("email@email.com");
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Host = "smtprelay.email.com";

//client.Credentials = new System.Net.NetworkCredential("email@email.com", password);
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

client.EnableSsl = true;
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;

try
{
client.Send(mail);
}
catch(Exception e)
{
Console.Write(e.Message);
}
mail.Dispose();
}


I tested with EnableSsl = true and false, neither worked. If I set UseDefaultCredentials to false, and give them via NetworkCredential, then it works.



Is there some setting in exchange or such that needs to be set?



Edit: int the ServerCertificateCallback, I get:
'((System.Net.Mail.SmtpClient)s).ServicePoint.Address' threw an exception of type 'System.NotSupportedException'



and sslPolicyErrors was System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch



Does that mean the group policies do not allow this?










share|improve this question





























    0















    I am trying to send an email using default credentials and the SMTPClient.



    The exception I get is:




    The SMTP Server requires a secured connection, or the client was not authenticated. The serverresponse was: 5.7.1 Client was not authenticated.




    The code I am trying to use:



    public void SendEmail(List<string> recipients, string subject, string body)
    {
    if (recipients.Count == 0)
    return;

    MailMessage mail = new MailMessage();
    SmtpClient client = new SmtpClient();
    foreach (string to in recipients)
    {
    mail.To.Add(to);
    }

    mail.From = new MailAddress("email@email.com");
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = true;
    client.Host = "smtprelay.email.com";

    //client.Credentials = new System.Net.NetworkCredential("email@email.com", password);
    ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

    client.EnableSsl = true;
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;

    try
    {
    client.Send(mail);
    }
    catch(Exception e)
    {
    Console.Write(e.Message);
    }
    mail.Dispose();
    }


    I tested with EnableSsl = true and false, neither worked. If I set UseDefaultCredentials to false, and give them via NetworkCredential, then it works.



    Is there some setting in exchange or such that needs to be set?



    Edit: int the ServerCertificateCallback, I get:
    '((System.Net.Mail.SmtpClient)s).ServicePoint.Address' threw an exception of type 'System.NotSupportedException'



    and sslPolicyErrors was System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch



    Does that mean the group policies do not allow this?










    share|improve this question



























      0












      0








      0








      I am trying to send an email using default credentials and the SMTPClient.



      The exception I get is:




      The SMTP Server requires a secured connection, or the client was not authenticated. The serverresponse was: 5.7.1 Client was not authenticated.




      The code I am trying to use:



      public void SendEmail(List<string> recipients, string subject, string body)
      {
      if (recipients.Count == 0)
      return;

      MailMessage mail = new MailMessage();
      SmtpClient client = new SmtpClient();
      foreach (string to in recipients)
      {
      mail.To.Add(to);
      }

      mail.From = new MailAddress("email@email.com");
      client.Port = 25;
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      client.UseDefaultCredentials = true;
      client.Host = "smtprelay.email.com";

      //client.Credentials = new System.Net.NetworkCredential("email@email.com", password);
      ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

      client.EnableSsl = true;
      mail.Subject = subject;
      mail.Body = body;
      mail.IsBodyHtml = true;

      try
      {
      client.Send(mail);
      }
      catch(Exception e)
      {
      Console.Write(e.Message);
      }
      mail.Dispose();
      }


      I tested with EnableSsl = true and false, neither worked. If I set UseDefaultCredentials to false, and give them via NetworkCredential, then it works.



      Is there some setting in exchange or such that needs to be set?



      Edit: int the ServerCertificateCallback, I get:
      '((System.Net.Mail.SmtpClient)s).ServicePoint.Address' threw an exception of type 'System.NotSupportedException'



      and sslPolicyErrors was System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch



      Does that mean the group policies do not allow this?










      share|improve this question
















      I am trying to send an email using default credentials and the SMTPClient.



      The exception I get is:




      The SMTP Server requires a secured connection, or the client was not authenticated. The serverresponse was: 5.7.1 Client was not authenticated.




      The code I am trying to use:



      public void SendEmail(List<string> recipients, string subject, string body)
      {
      if (recipients.Count == 0)
      return;

      MailMessage mail = new MailMessage();
      SmtpClient client = new SmtpClient();
      foreach (string to in recipients)
      {
      mail.To.Add(to);
      }

      mail.From = new MailAddress("email@email.com");
      client.Port = 25;
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      client.UseDefaultCredentials = true;
      client.Host = "smtprelay.email.com";

      //client.Credentials = new System.Net.NetworkCredential("email@email.com", password);
      ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

      client.EnableSsl = true;
      mail.Subject = subject;
      mail.Body = body;
      mail.IsBodyHtml = true;

      try
      {
      client.Send(mail);
      }
      catch(Exception e)
      {
      Console.Write(e.Message);
      }
      mail.Dispose();
      }


      I tested with EnableSsl = true and false, neither worked. If I set UseDefaultCredentials to false, and give them via NetworkCredential, then it works.



      Is there some setting in exchange or such that needs to be set?



      Edit: int the ServerCertificateCallback, I get:
      '((System.Net.Mail.SmtpClient)s).ServicePoint.Address' threw an exception of type 'System.NotSupportedException'



      and sslPolicyErrors was System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch



      Does that mean the group policies do not allow this?







      c# smtp exchange-server






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 16:32







      SinisterMJ

















      asked Nov 14 '18 at 15:22









      SinisterMJSinisterMJ

      2,1402138




      2,1402138
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Exchange requires either a valid domain account (login/pw) or an IP address that your postmaster has configured as allowable.






          share|improve this answer































            0














            you can try this code below



            public static void logMail(string err)
            {
            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new System.Net.NetworkCredential("email@email.com", "password");
            smtp.Port = 25;
            smtp.Host = "mail.email.com";

            MailAddress from = new MailAddress("email@email.com", "test");
            MailAddress to = new MailAddress("vb_error@email.com");
            MailMessage mail = new MailMessage(from, to);
            mail.IsBodyHtml = true;
            mail.Subject = "Error";
            mail.Body = "<b>Error Message:</b> " + err;
            smtp.Send(mail);
            }





            share|improve this answer
























            • Oh, I know that it works when I provide the NetworkCredentials. But its supposed to run without us altering the app.config XML every single time the policies require a change in password.

              – SinisterMJ
              Nov 14 '18 at 15:39











            • @SinisterMJ If you don't want to use credentials, you need to have the postmaster configure Exchange to allow your machine without authentication.

              – Terry Carmen
              Nov 14 '18 at 16:05











            • @TerryCarmen but then what is the default credentials? I mean, I can use Outlook on my system just fine, and it uses the Windows Login to access the mails, so why can't my code do the same?

              – SinisterMJ
              Nov 14 '18 at 16:11











            • SMTPClient is obsolete. It may not actually work. Contact your postmsaster and ask him what the specific error message was. S/he should be able to tell you what's wrong.

              – Terry Carmen
              Nov 14 '18 at 16:20











            • If it is obsolete, what is the current C# way to send an email using the exchange account of the logged in user?

              – SinisterMJ
              Nov 14 '18 at 16:30











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303502%2fc-sharp-sending-an-email-using-default-credentials-exception%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Exchange requires either a valid domain account (login/pw) or an IP address that your postmaster has configured as allowable.






            share|improve this answer




























              0














              Exchange requires either a valid domain account (login/pw) or an IP address that your postmaster has configured as allowable.






              share|improve this answer


























                0












                0








                0







                Exchange requires either a valid domain account (login/pw) or an IP address that your postmaster has configured as allowable.






                share|improve this answer













                Exchange requires either a valid domain account (login/pw) or an IP address that your postmaster has configured as allowable.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 15:33









                Terry CarmenTerry Carmen

                2,5591818




                2,5591818

























                    0














                    you can try this code below



                    public static void logMail(string err)
                    {
                    SmtpClient smtp = new SmtpClient();
                    smtp.Credentials = new System.Net.NetworkCredential("email@email.com", "password");
                    smtp.Port = 25;
                    smtp.Host = "mail.email.com";

                    MailAddress from = new MailAddress("email@email.com", "test");
                    MailAddress to = new MailAddress("vb_error@email.com");
                    MailMessage mail = new MailMessage(from, to);
                    mail.IsBodyHtml = true;
                    mail.Subject = "Error";
                    mail.Body = "<b>Error Message:</b> " + err;
                    smtp.Send(mail);
                    }





                    share|improve this answer
























                    • Oh, I know that it works when I provide the NetworkCredentials. But its supposed to run without us altering the app.config XML every single time the policies require a change in password.

                      – SinisterMJ
                      Nov 14 '18 at 15:39











                    • @SinisterMJ If you don't want to use credentials, you need to have the postmaster configure Exchange to allow your machine without authentication.

                      – Terry Carmen
                      Nov 14 '18 at 16:05











                    • @TerryCarmen but then what is the default credentials? I mean, I can use Outlook on my system just fine, and it uses the Windows Login to access the mails, so why can't my code do the same?

                      – SinisterMJ
                      Nov 14 '18 at 16:11











                    • SMTPClient is obsolete. It may not actually work. Contact your postmsaster and ask him what the specific error message was. S/he should be able to tell you what's wrong.

                      – Terry Carmen
                      Nov 14 '18 at 16:20











                    • If it is obsolete, what is the current C# way to send an email using the exchange account of the logged in user?

                      – SinisterMJ
                      Nov 14 '18 at 16:30
















                    0














                    you can try this code below



                    public static void logMail(string err)
                    {
                    SmtpClient smtp = new SmtpClient();
                    smtp.Credentials = new System.Net.NetworkCredential("email@email.com", "password");
                    smtp.Port = 25;
                    smtp.Host = "mail.email.com";

                    MailAddress from = new MailAddress("email@email.com", "test");
                    MailAddress to = new MailAddress("vb_error@email.com");
                    MailMessage mail = new MailMessage(from, to);
                    mail.IsBodyHtml = true;
                    mail.Subject = "Error";
                    mail.Body = "<b>Error Message:</b> " + err;
                    smtp.Send(mail);
                    }





                    share|improve this answer
























                    • Oh, I know that it works when I provide the NetworkCredentials. But its supposed to run without us altering the app.config XML every single time the policies require a change in password.

                      – SinisterMJ
                      Nov 14 '18 at 15:39











                    • @SinisterMJ If you don't want to use credentials, you need to have the postmaster configure Exchange to allow your machine without authentication.

                      – Terry Carmen
                      Nov 14 '18 at 16:05











                    • @TerryCarmen but then what is the default credentials? I mean, I can use Outlook on my system just fine, and it uses the Windows Login to access the mails, so why can't my code do the same?

                      – SinisterMJ
                      Nov 14 '18 at 16:11











                    • SMTPClient is obsolete. It may not actually work. Contact your postmsaster and ask him what the specific error message was. S/he should be able to tell you what's wrong.

                      – Terry Carmen
                      Nov 14 '18 at 16:20











                    • If it is obsolete, what is the current C# way to send an email using the exchange account of the logged in user?

                      – SinisterMJ
                      Nov 14 '18 at 16:30














                    0












                    0








                    0







                    you can try this code below



                    public static void logMail(string err)
                    {
                    SmtpClient smtp = new SmtpClient();
                    smtp.Credentials = new System.Net.NetworkCredential("email@email.com", "password");
                    smtp.Port = 25;
                    smtp.Host = "mail.email.com";

                    MailAddress from = new MailAddress("email@email.com", "test");
                    MailAddress to = new MailAddress("vb_error@email.com");
                    MailMessage mail = new MailMessage(from, to);
                    mail.IsBodyHtml = true;
                    mail.Subject = "Error";
                    mail.Body = "<b>Error Message:</b> " + err;
                    smtp.Send(mail);
                    }





                    share|improve this answer













                    you can try this code below



                    public static void logMail(string err)
                    {
                    SmtpClient smtp = new SmtpClient();
                    smtp.Credentials = new System.Net.NetworkCredential("email@email.com", "password");
                    smtp.Port = 25;
                    smtp.Host = "mail.email.com";

                    MailAddress from = new MailAddress("email@email.com", "test");
                    MailAddress to = new MailAddress("vb_error@email.com");
                    MailMessage mail = new MailMessage(from, to);
                    mail.IsBodyHtml = true;
                    mail.Subject = "Error";
                    mail.Body = "<b>Error Message:</b> " + err;
                    smtp.Send(mail);
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 15:37









                    Azat BozkurtAzat Bozkurt

                    1




                    1













                    • Oh, I know that it works when I provide the NetworkCredentials. But its supposed to run without us altering the app.config XML every single time the policies require a change in password.

                      – SinisterMJ
                      Nov 14 '18 at 15:39











                    • @SinisterMJ If you don't want to use credentials, you need to have the postmaster configure Exchange to allow your machine without authentication.

                      – Terry Carmen
                      Nov 14 '18 at 16:05











                    • @TerryCarmen but then what is the default credentials? I mean, I can use Outlook on my system just fine, and it uses the Windows Login to access the mails, so why can't my code do the same?

                      – SinisterMJ
                      Nov 14 '18 at 16:11











                    • SMTPClient is obsolete. It may not actually work. Contact your postmsaster and ask him what the specific error message was. S/he should be able to tell you what's wrong.

                      – Terry Carmen
                      Nov 14 '18 at 16:20











                    • If it is obsolete, what is the current C# way to send an email using the exchange account of the logged in user?

                      – SinisterMJ
                      Nov 14 '18 at 16:30



















                    • Oh, I know that it works when I provide the NetworkCredentials. But its supposed to run without us altering the app.config XML every single time the policies require a change in password.

                      – SinisterMJ
                      Nov 14 '18 at 15:39











                    • @SinisterMJ If you don't want to use credentials, you need to have the postmaster configure Exchange to allow your machine without authentication.

                      – Terry Carmen
                      Nov 14 '18 at 16:05











                    • @TerryCarmen but then what is the default credentials? I mean, I can use Outlook on my system just fine, and it uses the Windows Login to access the mails, so why can't my code do the same?

                      – SinisterMJ
                      Nov 14 '18 at 16:11











                    • SMTPClient is obsolete. It may not actually work. Contact your postmsaster and ask him what the specific error message was. S/he should be able to tell you what's wrong.

                      – Terry Carmen
                      Nov 14 '18 at 16:20











                    • If it is obsolete, what is the current C# way to send an email using the exchange account of the logged in user?

                      – SinisterMJ
                      Nov 14 '18 at 16:30

















                    Oh, I know that it works when I provide the NetworkCredentials. But its supposed to run without us altering the app.config XML every single time the policies require a change in password.

                    – SinisterMJ
                    Nov 14 '18 at 15:39





                    Oh, I know that it works when I provide the NetworkCredentials. But its supposed to run without us altering the app.config XML every single time the policies require a change in password.

                    – SinisterMJ
                    Nov 14 '18 at 15:39













                    @SinisterMJ If you don't want to use credentials, you need to have the postmaster configure Exchange to allow your machine without authentication.

                    – Terry Carmen
                    Nov 14 '18 at 16:05





                    @SinisterMJ If you don't want to use credentials, you need to have the postmaster configure Exchange to allow your machine without authentication.

                    – Terry Carmen
                    Nov 14 '18 at 16:05













                    @TerryCarmen but then what is the default credentials? I mean, I can use Outlook on my system just fine, and it uses the Windows Login to access the mails, so why can't my code do the same?

                    – SinisterMJ
                    Nov 14 '18 at 16:11





                    @TerryCarmen but then what is the default credentials? I mean, I can use Outlook on my system just fine, and it uses the Windows Login to access the mails, so why can't my code do the same?

                    – SinisterMJ
                    Nov 14 '18 at 16:11













                    SMTPClient is obsolete. It may not actually work. Contact your postmsaster and ask him what the specific error message was. S/he should be able to tell you what's wrong.

                    – Terry Carmen
                    Nov 14 '18 at 16:20





                    SMTPClient is obsolete. It may not actually work. Contact your postmsaster and ask him what the specific error message was. S/he should be able to tell you what's wrong.

                    – Terry Carmen
                    Nov 14 '18 at 16:20













                    If it is obsolete, what is the current C# way to send an email using the exchange account of the logged in user?

                    – SinisterMJ
                    Nov 14 '18 at 16:30





                    If it is obsolete, what is the current C# way to send an email using the exchange account of the logged in user?

                    – SinisterMJ
                    Nov 14 '18 at 16:30


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303502%2fc-sharp-sending-an-email-using-default-credentials-exception%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python