简体   繁体   中英

How Can I Fix System.Net.Mail.SmtpException

This is Image Of Error Here is Full Problem System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.

    `private void button1_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, body.Text);

        SmtpClient client = new SmtpClient(smtp.Text, 587);

        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);

        client.EnableSsl = true;
        client.Send(mail);
        MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
    }`

Here is The Code and I tried al Thing please tell How can I Fix it Email Sender

Because you use custom credentials and use authentication (enablessl = true). Together with your own login (not default) you need to add:

client.UseDefaultCredentials = false;

before setting client.credentials

Otherwise the credentials you filled in won't be used

EDIT: when this line is added as stated before, and there are still authentication problems. Try checking if all of the variables are correct. This can be done using the website: https://www.smtper.net/

Technically the variables should be tested before you start programming.

The error you are getting 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.' means that you aren't using HTTPS and/or can't authenticate using the credentials. smtper.net is therefor the best tool of finding out if your credentials are correct.

Your code seems to be correct on first glance with the exception of adding client.usedeafultcredentials = false. Wich is why I'm assuming the variables you are working with are incorrect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM