Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
So, I am trying to send mail with SMTP from my Windows Form Porject. It perfectly works for hotmail(""smtp.live.com"") but I cann't make it work with gmail. Here's my Code:

C#
public static void sendMailGmail(string from, string to, string subject, string text, string password)
        {
            MailMessage mail = new MailMessage(from, to, subject, text);
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.Port = 587;
            client.EnableSsl = true;
            
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(from, password);
            try
            {
                client.Send(mail);
                MessageBox.Show("Mesage has benn sant&quot");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failure while sending message&quot");
                MessageBox.Show(ex.Message);
            }
        }

So, where I am making a mistake ?
Posted

You are not the first, and no doubt will not be the last: SMTP from my Windows Form - Google Search[^].
 
Share this answer
 
Solution is to turn on "allow less secure apps" in my Gmail Account.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900