简体   繁体   中英

Unable to send email in asp.net C#

I understand this topic has been well elaborated previously, but I have looked around the Internet for various solutions and nothing has helped so far.

I'm making a web application which at one point is supposed to send an email to the email that the user provided. I'm trying to send it from my gmail account.

I tried the following:

public void test()
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("mygmail@gmail.com", "password*"),
                EnableSsl = true
            };
            client.Send("mygmail@gmail.com", "mygmail@gmail.com", "test", "testbody");
            Console.WriteLine("Sent");
            Console.ReadLine();
        }

And I'm getting the following error: The server response was: 5.5.1 Authentication Required.

In my web.config file I wrote

 <system.net>
    <mailSettings>
        <smtp>
            <network host="smtp.google.com" password="" userName=""/>
        </smtp>
    </mailSettings>
    </system.net> 

But that didn't work. I also tried adding the password* and the username ( mygmail@gmail.com ) in the web.config instead of the empty strings for userName and password, but I still got the same error.

Could anyone please help me out? Thank you.

Try setting UseDefaultCredentials to false before specifying your custom credentials. See c# SmtpClient class not able to send email using gmail

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