Click here to Skip to main content
14,458,710 members
Rate this:
Please Sign up or sign in to vote.
See more:
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("");
            mail.Subject = txtsub.Text;
            mail.Body = txtmsg.Text;

            SmtpClient Smtpserver = new SmtpClient();

            Smtpserver.Credentials = new NetworkCredential("", "password", "");
            Smtpserver.Host = "";
            Smtpserver.Port = 587;
            Smtpserver.DeliveryMethod = SmtpDeliveryMethod.Network;

            Smtpserver.EnableSsl = true;
            Smtpserver.UseDefaultCredentials = false;


            label1.Text = "Email successfully sent";
        catch (Exception ex)
            label1.Text = "Email failed" + ex.Message;

web.config file

    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
                <smtp deliveryMethod="Network" from="">
                    <network defaultCredentials="false" host="" port="25" userName="" password="password"/>

What I have tried:

i change my gmail account setting but cant get any success
Updated 22-Oct-19 23:31pm
Rate this:
Please Sign up or sign in to vote.

Solution 2

Manage your account access and security settings

Sign in - Google Accounts[^]
Further more
Read this to learn how to allow less secure APPs to use your login:

Try the Below Code
using (MailMessage mail = new MailMessage())
                mail.From = new MailAddress("From Mail-ID");
                mail.To.Add("To Mail-ID");
                mail.CC.Add("CC Mail-ID");//if required
                mail.Subject = "Subject of the Mail";
                mail.Body = "Body Content of the Mail";
                mail.IsBodyHtml = true;
                using (SmtpClient smtp = new SmtpClient())
                    smtp.Host = "";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential("From Mail-ID", MailPassword);

Hope this Help !
Rate this:
Please Sign up or sign in to vote.

Solution 1

According to the thread smtp - How to send an e-mail with C# through Gmail - Stack Overflow[^] it may help to execute the line
Smtpserver.UseDefaultCredentials = false;

before setting the credentials.

You are also specifying port 25 in your web.config file but that should not care here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100