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


            mail.From = new MailAddress("sami.goswami@gmail.com");
            mail.To.Add(txtto.Text);
            mail.Subject = txtsub.Text;
            mail.Body = txtmsg.Text;

            SmtpClient Smtpserver = new SmtpClient();

            Smtpserver.Credentials = new NetworkCredential("sami.goswami@gmail.com", "password", "smtp.gmail.com");
            Smtpserver.Host = "smtp.gmail.com";
            Smtpserver.Port = 587;
            Smtpserver.DeliveryMethod = SmtpDeliveryMethod.Network;

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

            Smtpserver.Send(mail);


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





web.config file

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  
  </system.web>
<system.net>
            <mailSettings>
                <smtp deliveryMethod="Network" from="sami.goswami@gmail.com">
                    <network defaultCredentials="false" host="smtp.gmail.com" port="25" userName="sami.goswami@gmail.com" password="password"/>
                </smtp>
            </mailSettings>
        </system.net>
</configuration>


What I have tried:

i change my gmail account setting but cant get any success
Posted
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:
https://support.google.com/accounts/answer/6010255?hl=en

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.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential("From Mail-ID", MailPassword);
                    smtp.Send(mail);
                }
            }

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