Send email form Gmail account using C#

kadir koçaslan 65 Reputation points
2023-02-03T11:30:34.83+00:00

When sending an email using the followng code, I'm getting an error, I'm asking for your help.

Authentication Required. Learn more at

string adSoyad = txtAdSoyad.Text;
string konu = "Koçaslanlar Web Konu başlığı";
string epostaAdresi = txtMail.Text;
string mesaji = txtMessage.Text;
string ipAdresi = ":44372"; //gönderenin IP adresini alıyoruz (localde 127.0.0.1 yada ::1 şeklinde gözükür)
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
    ipAdresi = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
    ipAdresi = HttpContext.Current.Request.UserHostAddress;

string mesajIcerik = "";
mesajIcerik += "<b>İletişim Formundan Gelen Mesaj</b> / " + DateTime.Now.ToString() + " / " + ipAdresi + "<br/>";
mesajIcerik += "<b>Adı Soyadı: </b>" + adSoyad + "<br/>";
mesajIcerik += "<b>E-posta: </b>" + epostaAdresi + "<br/>";
mesajIcerik += "<b>Konu: </b>" + konu + "<br/>";
mesajIcerik += "<b>İleti: </b>" + mesaji;

MailMessage yeniMesaj = new MailMessage();
yeniMesaj.IsBodyHtml = true;
yeniMesaj.To.Add(epostaAdresi);//buraya alıcı mail adresiniz gelecek
yeniMesaj.From = new MailAddress("test@gmail.com", "KOÇASLANLAR HOLDİNG", System.Text.Encoding.UTF8);
yeniMesaj.Subject = "Yeni Mesaj: " + adSoyad + " - " + konu;
yeniMesaj.Body = mesajIcerik;

SmtpClient gonder = new SmtpClient();

gonder.Credentials = new System.Net.NetworkCredential("test@gmail.com", "password");
gonder.Port = 587;
gonder.Host = "smtp.gmail.com";
gonder.EnableSsl = true;
gonder.UseDefaultCredentials = false;
gonder.Send(yeniMesaj);
lblMessage.Text = "send message!";
lblMessage.ForeColor = System.Drawing.ColorTranslator.FromHtml("#00a186");
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,185 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,978 questions
{count} votes

Accepted answer
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-02-03T16:06:29.01+00:00

    Looking into the code, it looks like you want to send email from Gmail. So you first need to do the following steps:

    1. Go to https://myaccount.google.com/security
    2. Enable 2-Step Verification
    3. Click on App passwords and add a new App, let's say "MySampleApp" and generate a password for it. Copy the password (it's 16 characters without any space).
    4. Then you can use this password from sending email on behalf of yourself.

    Now you can use the following code to send email:

    using (var client = new SmtpClient())
    {
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential("YOU@GMAIL.COM", "APP_PASSWORD");
        using (var message = new MailMessage(
            from: new MailAddress("YOU@GMAIL.COM", "YOUR NAME"),
            to: new MailAddress("THEM@ANYWHERE.COM", "THEIR NAME")
            ))
        {
    
            message.Subject = "Hello from code!";
            message.Body = "Loremn ipsum dolor sit amet ...";
    
            client.Send(message);
        }
    }
    
    3 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful