none
Error while authentication using C# console app RRS feed

  • Question

  • Hi everyone,

    I have C# console app to send automatic emails. I have local email server, and I can't authenticate to it.

    Here is my code:

    SmtpClient client = new SmtpClient("[IP address]", [port]);
    client.EnableSsl = false;
    MailAddress from = new MailAddress("[mailfrom]");
    MailAddress to = new MailAddress("[some]@gmail.com");
    MailMessage message = new MailMessage(from, to);
    message.Body = "This is a test e-mail message sent using a relay server ";
    message.Subject = "test email";
    client.UseDefaultCredentials = false;
    NetworkCredential myCreds = new NetworkCredential("[mailfrom]", "[password]");
    client.Credentials = myCreds;
    client.Send(message);

    At the end, I get following error:

    

    How can I connect to it? 

    There is no restrictions on email server, i need only to authenticate.

    Regards,

    Yerkhan

    Monday, October 14, 2019 5:53 AM

Answers

  • System administration said, that email server blocks anonymous connections, but since I use credentials, I thought that it would pass this barrier.

    Not necessarily. Just because you "use credentials" may not be sufficient to pass the "not anonymous" barrier. If you pas a type of credential that is not supported by the server, it will treat you as "no credentials passed".

    You need to find from System Administration what kind of credentials are required. For instance, they may only allow domain credentials (in which case you have to use new NetworkCredential(user, password, domain)), or they may only allow Windows Integrated authentication (in which case you would need client.UseDefaultCredentials=true and not pass .Credentials). And, of course, in both cases the credentials that you are using need to be allowed by the server.

    • Marked as answer by Yerkhan Tuesday, October 15, 2019 7:22 AM
    Monday, October 14, 2019 1:37 PM
    Moderator

All replies

  • The error message is telling you that your mail server did not accept the credentials that you passed as "[mailform]","[password]". This indicates that the server is not properly configured to accept this authentication. Maybe you have it set to only accept windows integrated security, or to require SSL, or there is some other type of restriction on the server side that prevents it from accepting these credentials.
    Monday, October 14, 2019 6:31 AM
    Moderator
  • Hi Yerkhan,

    Thank you for posting here.

    You could try the following code.

    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    client.EnableSsl = true;

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 14, 2019 7:44 AM
    Moderator
  • Hi,

    I have not gmail account. I already tried with gmail, it works. But I need corporate email.

    Regards,

    Yerkhan

    Monday, October 14, 2019 10:28 AM
  • Hi,

    Thank you for your answer. System administration said, that email server blocks anonymous connections, but since I use credentials, I thought that it would pass this barrier.

    Regards,

    Yerkhan

    Monday, October 14, 2019 10:30 AM
  • System administration said, that email server blocks anonymous connections, but since I use credentials, I thought that it would pass this barrier.

    Not necessarily. Just because you "use credentials" may not be sufficient to pass the "not anonymous" barrier. If you pas a type of credential that is not supported by the server, it will treat you as "no credentials passed".

    You need to find from System Administration what kind of credentials are required. For instance, they may only allow domain credentials (in which case you have to use new NetworkCredential(user, password, domain)), or they may only allow Windows Integrated authentication (in which case you would need client.UseDefaultCredentials=true and not pass .Credentials). And, of course, in both cases the credentials that you are using need to be allowed by the server.

    • Marked as answer by Yerkhan Tuesday, October 15, 2019 7:22 AM
    Monday, October 14, 2019 1:37 PM
    Moderator
  • Hi Yerkhan,

    Thanks for the feedback.

    Different kinds of emails have different host name, so if you want to send emails automatically, it is necessary for you to provide the specific host name.

    >>But I need corporate email.

    How about your corporate?

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 15, 2019 1:15 AM
    Moderator
  • Alberto,

    Domain was required. Was able to authenticate.

    Thanks.

    Regards,

    Yerkhan

    Tuesday, October 15, 2019 7:22 AM